<!DOCTYPE html>
<html prefix="og: http://ogp.me/ns# article: http://ogp.me/ns/article# " lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>python-data-structures | 绿萝间</title>
<link href="../assets/css/all-nocdn.css" rel="stylesheet" type="text/css">
<link href="../assets/css/ipython.min.css" rel="stylesheet" type="text/css">
<link href="../assets/css/nikola_ipython.css" rel="stylesheet" type="text/css">
<meta name="theme-color" content="#5670d4">
<meta name="generator" content="Nikola (getnikola.com)">
<link rel="alternate" type="application/rss+xml" title="RSS" href="../rss.xml">
<link rel="canonical" href="https://muxuezi.github.io/posts/python-data-structures.html">
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
    tex2jax: {
        inlineMath: [ ['$','$'], ["\\(","\\)"] ],
        displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
        processEscapes: true
    },
    displayAlign: 'center', // Change this to 'center' to center equations.
    "HTML-CSS": {
        styles: {'.MathJax_Display': {"margin": 0}}
    }
});
</script><!--[if lt IE 9]><script src="../assets/js/html5.js"></script><![endif]--><meta name="author" content="Tao Junjie">
<link rel="prev" href="kivy-ch5-remote-desktop-app.html" title="kivy-ch5-remote-desktop-app" type="text/html">
<link rel="next" href="kivy-ch9-shmup-app.html" title="kivy-ch9-shmup-app" type="text/html">
<meta property="og:site_name" content="绿萝间">
<meta property="og:title" content="python-data-structures">
<meta property="og:url" content="https://muxuezi.github.io/posts/python-data-structures.html">
<meta property="og:description" content="本文介绍Python数据结构与算法，原文在Problem Solving with Algorithms and Data Structures






In [1]:

    
from IPython.display import HTML













Table 1: Common Functions for Big-O





f(n)
Name



11
Cons">
<meta property="og:type" content="article">
<meta property="article:published_time" content="2015-06-24T13:44:38+08:00">
<meta property="article:tag" content="CHS">
<meta property="article:tag" content="ipython">
<meta property="article:tag" content="Other">
<meta property="article:tag" content="Python">
</head>
<body>
<a href="#content" class="sr-only sr-only-focusable">Skip to main content</a>

<!-- Menubar -->

<nav class="navbar navbar-inverse navbar-static-top"><div class="container">
<!-- This keeps the margins nice -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-navbar" aria-controls="bs-navbar" aria-expanded="false">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="https://muxuezi.github.io/">

                <span id="blog-title">绿萝间</span>
            </a>
        </div>
<!-- /.navbar-header -->
        <div class="collapse navbar-collapse" id="bs-navbar" aria-expanded="false">
            <ul class="nav navbar-nav">
<li>
<a href="../archive.html">Archive</a>
                </li>
<li>
<a href="../categories/">Tags</a>
                </li>
<li>
<a href="../rss.xml">RSS feed</a>

                
            </li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
    <a href="python-data-structures.ipynb" id="sourcelink">Source</a>
    </li>

                
            </ul>
</div>
<!-- /.navbar-collapse -->
    </div>
<!-- /.container -->
</nav><!-- End of Menubar --><div class="container" id="content" role="main">
    <div class="body-content">
        <!--Body content-->
        <div class="row">
            
            
<article class="post-text h-entry hentry postpage" itemscope="itemscope" itemtype="http://schema.org/Article"><header><h1 class="p-name entry-title" itemprop="headline name"><a href="#" class="u-url">python-data-structures</a></h1>

        <div class="metadata">
            <p class="byline author vcard"><span class="byline-name fn">
                    Tao Junjie
            </span></p>
            <p class="dateline"><a href="#" rel="bookmark"><time class="published dt-published" datetime="2015-06-24T13:44:38+08:00" itemprop="datePublished" title="2015-06-24 13:44">2015-06-24 13:44</time></a></p>
            
        <p class="sourceline"><a href="python-data-structures.ipynb" id="sourcelink">Source</a></p>

        </div>
        

    </header><div class="e-content entry-content" itemprop="articleBody text">
    <div tabindex="-1" id="notebook" class="border-box-sizing">
    <div class="container" id="notebook-container">

<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>本文介绍Python数据结构与算法，原文在<a href="http://interactivepython.org/runestone/static/pythonds/index.html">Problem Solving with Algorithms and Data Structures</a></p>
<!-- TEASER_END -->
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [1]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">IPython.display</span> <span class="k">import</span> <span class="n">HTML</span>
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<table border="0" class="table" id="tbl-fntable">
<caption><strong>Table 1: Common Functions for Big-O</strong></caption>
<colgroup>
<col width="50%">
<col width="50%">
</colgroup>
<thead valign="bottom"><tr class="row-odd">
<th class="head"><strong>f(n)</strong></th>
<th class="head"><strong>Name</strong></th>
</tr></thead>
<tbody valign="top">
<tr class="row-even">
<td><span class="math"><span class="MathJax_Preview"></span><span class="MathJax" id="MathJax-Element-23-Frame" role="textbox" aria-readonly="true"><nobr><span class="math" id="MathJax-Span-181" style="width: 0.647em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.53em; height: 0px; font-size: 122%;"><span style="position: absolute; clip: rect(1.876em 1000.003em 2.872em -0.465em); top: -2.69em; left: 0.003em;"><span class="mrow" id="MathJax-Span-182"><span class="mn" id="MathJax-Span-183" style="font-family: MathJax_Main;">1</span></span><span style="display: inline-block; width: 0px; height: 2.696em;"></span></span></span><span style="border-left-width: 0.004em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 0.932em; vertical-align: -0.068em;"></span></span></nobr></span><script type="math/tex" id="MathJax-Element-23">1</script></span></td>
<td>Constant</td>
</tr>
<tr class="row-odd">
<td><span class="math"><span class="MathJax_Preview"></span><span class="MathJax" id="MathJax-Element-24-Frame" role="textbox" aria-readonly="true"><nobr><span class="math" id="MathJax-Span-184" style="width: 2.52em; display: inline-block;"><span style="display: inline-block; position: relative; width: 2.052em; height: 0px; font-size: 122%;"><span style="position: absolute; clip: rect(1.818em 1000.003em 3.106em -0.524em); top: -2.69em; left: 0.003em;"><span class="mrow" id="MathJax-Span-185"><span class="mi" id="MathJax-Span-186" style="font-family: MathJax_Main;">log</span><span class="mo" id="MathJax-Span-187"></span><span class="mi" id="MathJax-Span-188" style="font-family: MathJax_Math; font-style: italic; padding-left: 0.179em;">n</span></span><span style="display: inline-block; width: 0px; height: 2.696em;"></span></span></span><span style="border-left-width: 0.004em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 1.218em; vertical-align: -0.354em;"></span></span></nobr></span><script type="math/tex" id="MathJax-Element-24">\log n</script></span></td>
<td>Logarithmic</td>
</tr>
<tr class="row-even">
<td><span class="math"><span class="MathJax_Preview"></span><span class="MathJax" id="MathJax-Element-25-Frame" role="textbox" aria-readonly="true"><nobr><span class="math" id="MathJax-Span-189" style="width: 0.823em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.647em; height: 0px; font-size: 122%;"><span style="position: absolute; clip: rect(1.818em 1000.003em 2.638em -0.524em); top: -2.456em; left: 0.003em;"><span class="mrow" id="MathJax-Span-190"><span class="mi" id="MathJax-Span-191" style="font-family: MathJax_Math; font-style: italic;">n</span></span><span style="display: inline-block; width: 0px; height: 2.462em;"></span></span></span><span style="border-left-width: 0.004em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 0.718em; vertical-align: -0.068em;"></span></span></nobr></span><script type="math/tex" id="MathJax-Element-25">n</script></span></td>
<td>Linear</td>
</tr>
<tr class="row-odd">
<td><span class="math"><span class="MathJax_Preview"></span><span class="MathJax" id="MathJax-Element-26-Frame" role="textbox" aria-readonly="true"><nobr><span class="math" id="MathJax-Span-192" style="width: 3.457em; display: inline-block;"><span style="display: inline-block; position: relative; width: 2.813em; height: 0px; font-size: 122%;"><span style="position: absolute; clip: rect(1.818em 1000.003em 3.106em -0.524em); top: -2.69em; left: 0.003em;"><span class="mrow" id="MathJax-Span-193"><span class="mi" id="MathJax-Span-194" style="font-family: MathJax_Math; font-style: italic;">n</span><span class="mi" id="MathJax-Span-195" style="font-family: MathJax_Main; padding-left: 0.179em;">log</span><span class="mo" id="MathJax-Span-196"></span><span class="mi" id="MathJax-Span-197" style="font-family: MathJax_Math; font-style: italic; padding-left: 0.179em;">n</span></span><span style="display: inline-block; width: 0px; height: 2.696em;"></span></span></span><span style="border-left-width: 0.004em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 1.218em; vertical-align: -0.354em;"></span></span></nobr></span><script type="math/tex" id="MathJax-Element-26">n\log n</script></span></td>
<td>Log Linear</td>
</tr>
<tr class="row-even">
<td><span class="math"><span class="MathJax_Preview"></span><span class="MathJax" id="MathJax-Element-27-Frame" role="textbox" aria-readonly="true"><nobr><span class="math" id="MathJax-Span-198" style="width: 1.35em; display: inline-block;"><span style="display: inline-block; position: relative; width: 1.115em; height: 0px; font-size: 122%;"><span style="position: absolute; clip: rect(1.115em 1000.003em 2.286em -0.524em); top: -2.105em; left: 0.003em;"><span class="mrow" id="MathJax-Span-199"><span class="msubsup" id="MathJax-Span-200"><span style="display: inline-block; position: relative; width: 1.115em; height: 0px;"><span style="position: absolute; clip: rect(1.818em 1000.003em 2.638em -0.524em); top: -2.456em; left: 0.003em;"><span class="mi" id="MathJax-Span-201" style="font-family: MathJax_Math; font-style: italic;">n</span><span style="display: inline-block; width: 0px; height: 2.462em;"></span></span><span style="position: absolute; top: -2.632em; left: 0.647em;"><span class="texatom" id="MathJax-Span-202"><span class="mrow" id="MathJax-Span-203"><span class="mn" id="MathJax-Span-204" style="font-size: 70.7%; font-family: MathJax_Main;">2</span></span></span><span style="display: inline-block; width: 0px; height: 2.286em;"></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 2.111em;"></span></span></span><span style="border-left-width: 0.004em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 1.146em; vertical-align: -0.068em;"></span></span></nobr></span><script type="math/tex" id="MathJax-Element-27">n^{2}</script></span></td>
<td>Quadratic</td>
</tr>
<tr class="row-odd">
<td><span class="math"><span class="MathJax_Preview"></span><span class="MathJax" id="MathJax-Element-28-Frame" role="textbox" aria-readonly="true"><nobr><span class="math" id="MathJax-Span-205" style="width: 1.35em; display: inline-block;"><span style="display: inline-block; position: relative; width: 1.115em; height: 0px; font-size: 122%;"><span style="position: absolute; clip: rect(1.115em 1000.003em 2.286em -0.524em); top: -2.105em; left: 0.003em;"><span class="mrow" id="MathJax-Span-206"><span class="msubsup" id="MathJax-Span-207"><span style="display: inline-block; position: relative; width: 1.115em; height: 0px;"><span style="position: absolute; clip: rect(1.818em 1000.003em 2.638em -0.524em); top: -2.456em; left: 0.003em;"><span class="mi" id="MathJax-Span-208" style="font-family: MathJax_Math; font-style: italic;">n</span><span style="display: inline-block; width: 0px; height: 2.462em;"></span></span><span style="position: absolute; top: -2.632em; left: 0.647em;"><span class="texatom" id="MathJax-Span-209"><span class="mrow" id="MathJax-Span-210"><span class="mn" id="MathJax-Span-211" style="font-size: 70.7%; font-family: MathJax_Main;">3</span></span></span><span style="display: inline-block; width: 0px; height: 2.286em;"></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 2.111em;"></span></span></span><span style="border-left-width: 0.004em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 1.146em; vertical-align: -0.068em;"></span></span></nobr></span><script type="math/tex" id="MathJax-Element-28">n^{3}</script></span></td>
<td>Cubic</td>
</tr>
<tr class="row-even">
<td><span class="math"><span class="MathJax_Preview"></span><span class="MathJax" id="MathJax-Element-29-Frame" role="textbox" aria-readonly="true"><nobr><span class="math" id="MathJax-Span-212" style="width: 1.232em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.998em; height: 0px; font-size: 122%;"><span style="position: absolute; clip: rect(1.232em 1000.003em 2.286em -0.465em); top: -2.105em; left: 0.003em;"><span class="mrow" id="MathJax-Span-213"><span class="msubsup" id="MathJax-Span-214"><span style="display: inline-block; position: relative; width: 0.998em; height: 0px;"><span style="position: absolute; clip: rect(1.876em 1000.003em 2.872em -0.465em); top: -2.69em; left: 0.003em;"><span class="mn" id="MathJax-Span-215" style="font-family: MathJax_Main;">2</span><span style="display: inline-block; width: 0px; height: 2.696em;"></span></span><span style="position: absolute; top: -2.632em; left: 0.53em;"><span class="texatom" id="MathJax-Span-216"><span class="mrow" id="MathJax-Span-217"><span class="mi" id="MathJax-Span-218" style="font-size: 70.7%; font-family: MathJax_Math; font-style: italic;">n</span></span></span><span style="display: inline-block; width: 0px; height: 2.228em;"></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 2.111em;"></span></span></span><span style="border-left-width: 0.004em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 1.004em; vertical-align: -0.068em;"></span></span></nobr></span><script type="math/tex" id="MathJax-Element-29">2^{n}</script></span></td>
<td>Exponential</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<table border="0" class="table" id="tbl-listbigo">
<caption><strong>Table 2: Big-O Efficiency of Python List Operators</strong></caption>
<colgroup>
<col width="50%">
<col width="50%">
</colgroup>
<thead valign="bottom"><tr class="row-odd">
<th class="head">Operation</th>
<th class="head">Big-O Efficiency</th>
</tr></thead>
<tbody valign="top">
<tr class="row-even">
<td>index []</td>
<td>O(1)</td>
</tr>
<tr class="row-odd">
<td>index assignment</td>
<td>O(1)</td>
</tr>
<tr class="row-even">
<td>append</td>
<td>O(1)</td>
</tr>
<tr class="row-odd">
<td>pop()</td>
<td>O(1)</td>
</tr>
<tr class="row-even">
<td>pop(i)</td>
<td>O(n)</td>
</tr>
<tr class="row-odd">
<td>insert(i,item)</td>
<td>O(n)</td>
</tr>
<tr class="row-even">
<td>del operator</td>
<td>O(n)</td>
</tr>
<tr class="row-odd">
<td>iteration</td>
<td>O(n)</td>
</tr>
<tr class="row-even">
<td>contains (in)</td>
<td>O(n)</td>
</tr>
<tr class="row-odd">
<td>get slice [x:y]</td>
<td>O(k)</td>
</tr>
<tr class="row-even">
<td>del slice</td>
<td>O(n)</td>
</tr>
<tr class="row-odd">
<td>set slice</td>
<td>O(n+k)</td>
</tr>
<tr class="row-even">
<td>reverse</td>
<td>O(n)</td>
</tr>
<tr class="row-odd">
<td>concatenate</td>
<td>O(k)</td>
</tr>
<tr class="row-even">
<td>sort</td>
<td>O(n log n)</td>
</tr>
<tr class="row-odd">
<td>multiply</td>
<td>O(nk)</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<table border="0" class="table" id="tbl-dictbigo">
<caption><strong>Table 3: Big-O Efficiency of Python Dictionary Operations</strong></caption>
<colgroup>
<col width="50%">
<col width="50%">
</colgroup>
<thead valign="bottom"><tr class="row-odd">
<th class="head">operation</th>
<th class="head">Big-O Efficiency</th>
</tr></thead>
<tbody valign="top">
<tr class="row-even">
<td>copy</td>
<td>O(n)</td>
</tr>
<tr class="row-odd">
<td>get item</td>
<td>O(1)</td>
</tr>
<tr class="row-even">
<td>set item</td>
<td>O(1)</td>
</tr>
<tr class="row-odd">
<td>delete item</td>
<td>O(1)</td>
</tr>
<tr class="row-even">
<td>contains (in)</td>
<td>O(1)</td>
</tr>
<tr class="row-odd">
<td>iteration</td>
<td>O(n)</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="The-Stack-Abstract-Data-Type">The Stack Abstract Data Type<a class="anchor-link" href="python-data-structures.html#The-Stack-Abstract-Data-Type">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<table border="0" class="table" id="tbl-stackops">
<caption><strong>Table 1: Sample Stack Operations</strong></caption>
<colgroup>
<col width="40%">
<col width="34%">
<col width="26%">
</colgroup>
<thead valign="bottom"><tr class="row-odd">
<th class="head"><strong>Stack Operation</strong></th>
<th class="head"><strong>Stack Contents</strong></th>
<th class="head"><strong>Return Value</strong></th>
</tr></thead>
<tbody valign="top">
<tr class="row-even">
<td><code>s.isEmpty()</code></td>
<td><code>[]</code></td>
<td><code>True</code></td>
</tr>
<tr class="row-odd">
<td><code>s.push(4)</code></td>
<td><code>[4]</code></td>
<td> </td>
</tr>
<tr class="row-even">
<td><code>s.push('dog')</code></td>
<td><code>[4,'dog']</code></td>
<td> </td>
</tr>
<tr class="row-odd">
<td><code>s.peek()</code></td>
<td><code>[4,'dog']</code></td>
<td><code>'dog'</code></td>
</tr>
<tr class="row-even">
<td><code>s.push(True)</code></td>
<td><code>[4,'dog',True]</code></td>
<td> </td>
</tr>
<tr class="row-odd">
<td><code>s.size()</code></td>
<td><code>[4,'dog',True]</code></td>
<td><code>3</code></td>
</tr>
<tr class="row-even">
<td><code>s.isEmpty()</code></td>
<td><code>[4,'dog',True]</code></td>
<td><code>False</code></td>
</tr>
<tr class="row-odd">
<td><code>s.push(8.4)</code></td>
<td><code>[4,'dog',True,8.4]</code></td>
<td> </td>
</tr>
<tr class="row-even">
<td><code>s.pop()</code></td>
<td><code>[4,'dog',True]</code></td>
<td><code>8.4</code></td>
</tr>
<tr class="row-odd">
<td><code>s.pop()</code></td>
<td><code>[4,'dog']</code></td>
<td><code>True</code></td>
</tr>
<tr class="row-even">
<td><code>s.size()</code></td>
<td><code>[4,'dog']</code></td>
<td><code>2</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Implementing-a-Stack-in-Python">Implementing a Stack in Python<a class="anchor-link" href="python-data-structures.html#Implementing-a-Stack-in-Python">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [2]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">class</span> <span class="nc">Stack</span><span class="p">:</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">items</span> <span class="o">=</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="nf">isEmpty</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">items</span> <span class="o">==</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="nf">push</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">item</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">items</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">item</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">pop</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">items</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">peek</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">items</span><span class="p">[</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">items</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">size</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">items</span><span class="p">)</span>
    
    <span class="k">def</span> <span class="nf">show</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">size</span><span class="p">())]</span>
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [3]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">revstring</span><span class="p">(</span><span class="n">mystr</span><span class="p">):</span>
    <span class="c1"># your code here</span>
    <span class="n">t</span> <span class="o">=</span> <span class="n">Stack</span><span class="p">()</span>
    <span class="p">[</span><span class="n">t</span><span class="o">.</span><span class="n">push</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">mystr</span><span class="p">]</span>
    <span class="k">return</span> <span class="s1">''</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">t</span><span class="o">.</span><span class="n">show</span><span class="p">())</span>

<span class="n">revstring</span><span class="p">(</span><span class="s1">'apple'</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt output_prompt">Out[3]:</div>


<div class="output_text output_subarea output_execute_result">
<pre>'elppa'</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Simple-Balanced-Parentheses">Simple Balanced Parentheses<a class="anchor-link" href="python-data-structures.html#Simple-Balanced-Parentheses">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [4]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">pythonds.basic.stack</span> <span class="k">import</span> <span class="n">Stack</span>

<span class="k">def</span> <span class="nf">parChecker</span><span class="p">(</span><span class="n">symbolString</span><span class="p">):</span>
    <span class="n">s</span> <span class="o">=</span> <span class="n">Stack</span><span class="p">()</span>
    <span class="n">balanced</span> <span class="o">=</span> <span class="kc">True</span>
    <span class="n">index</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="k">while</span> <span class="n">index</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">symbolString</span><span class="p">)</span> <span class="ow">and</span> <span class="n">balanced</span><span class="p">:</span>
        <span class="n">symbol</span> <span class="o">=</span> <span class="n">symbolString</span><span class="p">[</span><span class="n">index</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">symbol</span> <span class="o">==</span> <span class="s2">"("</span><span class="p">:</span>
            <span class="n">s</span><span class="o">.</span><span class="n">push</span><span class="p">(</span><span class="n">symbol</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">s</span><span class="o">.</span><span class="n">isEmpty</span><span class="p">():</span>
                <span class="n">balanced</span> <span class="o">=</span> <span class="kc">False</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">s</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>

        <span class="n">index</span> <span class="o">=</span> <span class="n">index</span> <span class="o">+</span> <span class="mi">1</span>

    <span class="k">if</span> <span class="n">balanced</span> <span class="ow">and</span> <span class="n">s</span><span class="o">.</span><span class="n">isEmpty</span><span class="p">():</span>
        <span class="k">return</span> <span class="kc">True</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="kc">False</span>

<span class="nb">print</span><span class="p">(</span><span class="n">parChecker</span><span class="p">(</span><span class="s1">'((()))'</span><span class="p">))</span>
<span class="nb">print</span><span class="p">(</span><span class="n">parChecker</span><span class="p">(</span><span class="s1">'(()'</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>True
False
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Balanced-Symbols-(A-General-Case)">Balanced Symbols (A General Case)<a class="anchor-link" href="python-data-structures.html#Balanced-Symbols-(A-General-Case)">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [5]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">pythonds.basic.stack</span> <span class="k">import</span> <span class="n">Stack</span>

<span class="k">def</span> <span class="nf">parChecker</span><span class="p">(</span><span class="n">symbolString</span><span class="p">):</span>
    <span class="n">s</span> <span class="o">=</span> <span class="n">Stack</span><span class="p">()</span>
    <span class="n">balanced</span> <span class="o">=</span> <span class="kc">True</span>
    <span class="n">index</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="k">while</span> <span class="n">index</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">symbolString</span><span class="p">)</span> <span class="ow">and</span> <span class="n">balanced</span><span class="p">:</span>
        <span class="n">symbol</span> <span class="o">=</span> <span class="n">symbolString</span><span class="p">[</span><span class="n">index</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">symbol</span> <span class="ow">in</span> <span class="s2">"([{"</span><span class="p">:</span>
            <span class="n">s</span><span class="o">.</span><span class="n">push</span><span class="p">(</span><span class="n">symbol</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">s</span><span class="o">.</span><span class="n">isEmpty</span><span class="p">():</span>
                <span class="n">balanced</span> <span class="o">=</span> <span class="kc">False</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">top</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">matches</span><span class="p">(</span><span class="n">top</span><span class="p">,</span><span class="n">symbol</span><span class="p">):</span>
                       <span class="n">balanced</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="n">index</span> <span class="o">=</span> <span class="n">index</span> <span class="o">+</span> <span class="mi">1</span>
    <span class="k">if</span> <span class="n">balanced</span> <span class="ow">and</span> <span class="n">s</span><span class="o">.</span><span class="n">isEmpty</span><span class="p">():</span>
        <span class="k">return</span> <span class="kc">True</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="kc">False</span>

<span class="k">def</span> <span class="nf">matches</span><span class="p">(</span><span class="nb">open</span><span class="p">,</span><span class="n">close</span><span class="p">):</span>
    <span class="n">opens</span> <span class="o">=</span> <span class="s2">"([{"</span>
    <span class="n">closers</span> <span class="o">=</span> <span class="s2">")]}"</span>
    <span class="k">return</span> <span class="n">opens</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="nb">open</span><span class="p">)</span> <span class="o">==</span> <span class="n">closers</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">close</span><span class="p">)</span>


<span class="nb">print</span><span class="p">(</span><span class="n">parChecker</span><span class="p">(</span><span class="s1">'{{([][])}()}'</span><span class="p">))</span>
<span class="nb">print</span><span class="p">(</span><span class="n">parChecker</span><span class="p">(</span><span class="s1">'[{()]'</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>True
False
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Converting-Decimal-Numbers-to-Binary-Numbers">Converting Decimal Numbers to Binary Numbers<a class="anchor-link" href="python-data-structures.html#Converting-Decimal-Numbers-to-Binary-Numbers">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [6]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">pythonds.basic.stack</span> <span class="k">import</span> <span class="n">Stack</span>

<span class="k">def</span> <span class="nf">baseConverter</span><span class="p">(</span><span class="n">decNumber</span><span class="p">,</span><span class="n">base</span><span class="p">):</span>
    <span class="n">digits</span> <span class="o">=</span> <span class="s2">"0123456789ABCDEF"</span>

    <span class="n">remstack</span> <span class="o">=</span> <span class="n">Stack</span><span class="p">()</span>

    <span class="k">while</span> <span class="n">decNumber</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
        <span class="n">rem</span> <span class="o">=</span> <span class="n">decNumber</span> <span class="o">%</span> <span class="n">base</span>
        <span class="n">remstack</span><span class="o">.</span><span class="n">push</span><span class="p">(</span><span class="n">rem</span><span class="p">)</span>
        <span class="n">decNumber</span> <span class="o">=</span> <span class="n">decNumber</span> <span class="o">//</span> <span class="n">base</span>

    <span class="n">newString</span> <span class="o">=</span> <span class="s2">""</span>
    <span class="k">while</span> <span class="ow">not</span> <span class="n">remstack</span><span class="o">.</span><span class="n">isEmpty</span><span class="p">():</span>
        <span class="n">newString</span> <span class="o">=</span> <span class="n">newString</span> <span class="o">+</span> <span class="n">digits</span><span class="p">[</span><span class="n">remstack</span><span class="o">.</span><span class="n">pop</span><span class="p">()]</span>

    <span class="k">return</span> <span class="n">newString</span>

<span class="nb">print</span><span class="p">(</span><span class="n">baseConverter</span><span class="p">(</span><span class="mi">25</span><span class="p">,</span><span class="mi">2</span><span class="p">))</span>
<span class="nb">print</span><span class="p">(</span><span class="n">baseConverter</span><span class="p">(</span><span class="mi">25</span><span class="p">,</span><span class="mi">16</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>11001
19
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Infix,-Prefix-and-Postfix-Expressions">Infix, Prefix and Postfix Expressions<a class="anchor-link" href="python-data-structures.html#Infix,-Prefix-and-Postfix-Expressions">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<ol>
<li>Create an empty stack called opstack for keeping operators. Create an empty list for output.</li>
<li>Convert the input infix string to a list by using the string method split.</li>
<li>Scan the token list from left to right.<ul>
<li>If the token is an operand, append it to the end of the output list.</li>
<li>If the token is a left parenthesis, push it on the opstack.</li>
<li>If the token is a right parenthesis, pop the opstack until the corresponding left parenthesis is removed. Append each operator to the end of the output list.</li>
<li>If the token is an operator, *, /, +, or -, push it on the opstack. However, first remove any operators already on the opstack that have higher or equal precedence and append them to the output list.</li>
</ul>
</li>
<li>When the input expression has been completely processed, check the opstack. Any operators still on the stack can be removed and appended to the end of the output list.</li>
</ol>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [7]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">pythonds.basic.stack</span> <span class="k">import</span> <span class="n">Stack</span>

<span class="k">def</span> <span class="nf">infixToPostfix</span><span class="p">(</span><span class="n">infixexpr</span><span class="p">):</span>
    <span class="n">prec</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="n">prec</span><span class="p">[</span><span class="s2">"*"</span><span class="p">]</span> <span class="o">=</span> <span class="mi">3</span>
    <span class="n">prec</span><span class="p">[</span><span class="s2">"/"</span><span class="p">]</span> <span class="o">=</span> <span class="mi">3</span>
    <span class="n">prec</span><span class="p">[</span><span class="s2">"+"</span><span class="p">]</span> <span class="o">=</span> <span class="mi">2</span>
    <span class="n">prec</span><span class="p">[</span><span class="s2">"-"</span><span class="p">]</span> <span class="o">=</span> <span class="mi">2</span>
    <span class="n">prec</span><span class="p">[</span><span class="s2">"("</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>
    <span class="n">opStack</span> <span class="o">=</span> <span class="n">Stack</span><span class="p">()</span>
    <span class="n">postfixList</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">tokenList</span> <span class="o">=</span> <span class="n">infixexpr</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>

    <span class="k">for</span> <span class="n">token</span> <span class="ow">in</span> <span class="n">tokenList</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">token</span> <span class="ow">in</span> <span class="s2">"ABCDEFGHIJKLMNOPQRSTUVWXYZ"</span> <span class="ow">or</span> <span class="n">token</span> <span class="ow">in</span> <span class="s2">"0123456789"</span><span class="p">:</span>
            <span class="n">postfixList</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">token</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">token</span> <span class="o">==</span> <span class="s1">'('</span><span class="p">:</span>
            <span class="n">opStack</span><span class="o">.</span><span class="n">push</span><span class="p">(</span><span class="n">token</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">token</span> <span class="o">==</span> <span class="s1">')'</span><span class="p">:</span>
            <span class="n">topToken</span> <span class="o">=</span> <span class="n">opStack</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
            <span class="k">while</span> <span class="n">topToken</span> <span class="o">!=</span> <span class="s1">'('</span><span class="p">:</span>
                <span class="n">postfixList</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">topToken</span><span class="p">)</span>
                <span class="n">topToken</span> <span class="o">=</span> <span class="n">opStack</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">while</span> <span class="p">(</span><span class="ow">not</span> <span class="n">opStack</span><span class="o">.</span><span class="n">isEmpty</span><span class="p">())</span> <span class="ow">and</span> \
               <span class="p">(</span><span class="n">prec</span><span class="p">[</span><span class="n">opStack</span><span class="o">.</span><span class="n">peek</span><span class="p">()]</span> <span class="o">&gt;=</span> <span class="n">prec</span><span class="p">[</span><span class="n">token</span><span class="p">]):</span>
                  <span class="n">postfixList</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">opStack</span><span class="o">.</span><span class="n">pop</span><span class="p">())</span>
            <span class="n">opStack</span><span class="o">.</span><span class="n">push</span><span class="p">(</span><span class="n">token</span><span class="p">)</span>

    <span class="k">while</span> <span class="ow">not</span> <span class="n">opStack</span><span class="o">.</span><span class="n">isEmpty</span><span class="p">():</span>
        <span class="n">postfixList</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">opStack</span><span class="o">.</span><span class="n">pop</span><span class="p">())</span>
    <span class="k">return</span> <span class="s2">" "</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">postfixList</span><span class="p">)</span>

<span class="nb">print</span><span class="p">(</span><span class="n">infixToPostfix</span><span class="p">(</span><span class="s2">"A * B + C * D"</span><span class="p">))</span>
<span class="nb">print</span><span class="p">(</span><span class="n">infixToPostfix</span><span class="p">(</span><span class="s2">"( A + B ) * C - ( D - E ) * ( F + G )"</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>A B * C D * +
A B + C * D E - F G + * -
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Postfix-Evaluation">Postfix Evaluation<a class="anchor-link" href="python-data-structures.html#Postfix-Evaluation">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<ol>
<li>Create an empty stack called operandStack.</li>
<li>Convert the string to a list by using the string method split.</li>
<li>Scan the token list from left to right.<ul>
<li>If the token is an operand, convert it from a string to an integer and push the value onto the operandStack.</li>
<li>If the token is an operator, *, /, +, or -, it will need two operands. Pop the operandStack twice. The first pop is the second operand and the second pop is the first operand. Perform the arithmetic operation. Push the result back on the operandStack.</li>
</ul>
</li>
<li>When the input expression has been completely processed, the result is on the stack. Pop the operandStack and return the value.</li>
</ol>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [8]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">pythonds.basic.stack</span> <span class="k">import</span> <span class="n">Stack</span>

<span class="k">def</span> <span class="nf">postfixEval</span><span class="p">(</span><span class="n">postfixExpr</span><span class="p">):</span>
    <span class="n">operandStack</span> <span class="o">=</span> <span class="n">Stack</span><span class="p">()</span>
    <span class="n">tokenList</span> <span class="o">=</span> <span class="n">postfixExpr</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>

    <span class="k">for</span> <span class="n">token</span> <span class="ow">in</span> <span class="n">tokenList</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">token</span> <span class="ow">in</span> <span class="s2">"0123456789"</span><span class="p">:</span>
            <span class="n">operandStack</span><span class="o">.</span><span class="n">push</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">token</span><span class="p">))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">operand2</span> <span class="o">=</span> <span class="n">operandStack</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
            <span class="n">operand1</span> <span class="o">=</span> <span class="n">operandStack</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
            <span class="n">result</span> <span class="o">=</span> <span class="n">doMath</span><span class="p">(</span><span class="n">token</span><span class="p">,</span><span class="n">operand1</span><span class="p">,</span><span class="n">operand2</span><span class="p">)</span>
            <span class="n">operandStack</span><span class="o">.</span><span class="n">push</span><span class="p">(</span><span class="n">result</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">operandStack</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>

<span class="k">def</span> <span class="nf">doMath</span><span class="p">(</span><span class="n">op</span><span class="p">,</span> <span class="n">op1</span><span class="p">,</span> <span class="n">op2</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">op</span> <span class="o">==</span> <span class="s2">"*"</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">op1</span> <span class="o">*</span> <span class="n">op2</span>
    <span class="k">elif</span> <span class="n">op</span> <span class="o">==</span> <span class="s2">"/"</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">op1</span> <span class="o">/</span> <span class="n">op2</span>
    <span class="k">elif</span> <span class="n">op</span> <span class="o">==</span> <span class="s2">"+"</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">op1</span> <span class="o">+</span> <span class="n">op2</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">op1</span> <span class="o">-</span> <span class="n">op2</span>

<span class="nb">print</span><span class="p">(</span><span class="n">postfixEval</span><span class="p">(</span><span class="s1">'7 8 + 3 2 + /'</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>3.0
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="The-Queue-Abstract-Data-Type">The Queue Abstract Data Type<a class="anchor-link" href="python-data-structures.html#The-Queue-Abstract-Data-Type">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<table border="0" class="table" id="tbl-queueoperations">
<caption><strong>Table 1: Example Queue Operations</strong></caption>
<colgroup>
<col width="40%">
<col width="34%">
<col width="26%">
</colgroup>
<thead valign="bottom"><tr class="row-odd">
<th class="head"><strong>Queue Operation</strong></th>
<th class="head"><strong>Queue Contents</strong></th>
<th class="head"><strong>Return Value</strong></th>
</tr></thead>
<tbody valign="top">
<tr class="row-even">
<td><code>q.isEmpty()</code></td>
<td><code>[]</code></td>
<td><code>True</code></td>
</tr>
<tr class="row-odd">
<td><code>q.enqueue(4)</code></td>
<td><code>[4]</code></td>
<td> </td>
</tr>
<tr class="row-even">
<td><code>q.enqueue('dog')</code></td>
<td><code>['dog',4]</code></td>
<td> </td>
</tr>
<tr class="row-odd">
<td><code>q.enqueue(True)</code></td>
<td><code>[True,'dog',4]</code></td>
<td> </td>
</tr>
<tr class="row-even">
<td><code>q.size()</code></td>
<td><code>[True,'dog',4]</code></td>
<td><code>3</code></td>
</tr>
<tr class="row-odd">
<td><code>q.isEmpty()</code></td>
<td><code>[True,'dog',4]</code></td>
<td><code>False</code></td>
</tr>
<tr class="row-even">
<td><code>q.enqueue(8.4)</code></td>
<td><code>[8.4,True,'dog',4]</code></td>
<td> </td>
</tr>
<tr class="row-odd">
<td><code>q.dequeue()</code></td>
<td><code>[8.4,True,'dog']</code></td>
<td><code>4</code></td>
</tr>
<tr class="row-even">
<td><code>q.dequeue()</code></td>
<td><code>[8.4,True]</code></td>
<td><code>'dog'</code></td>
</tr>
<tr class="row-odd">
<td><code>q.size()</code></td>
<td><code>[8.4,True]</code></td>
<td><code>2</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Implementing-a-Queue-in-Python">Implementing a Queue in Python<a class="anchor-link" href="python-data-structures.html#Implementing-a-Queue-in-Python">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [9]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">class</span> <span class="nc">Queue</span><span class="p">:</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">items</span> <span class="o">=</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="nf">isEmpty</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">items</span> <span class="o">==</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="nf">enqueue</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">item</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">items</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="n">item</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">dequeue</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">items</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">size</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">items</span><span class="p">)</span>
    
    <span class="k">def</span> <span class="nf">show</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">dequeue</span><span class="p">(),</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">size</span><span class="p">()))</span>
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [10]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">q</span> <span class="o">=</span> <span class="n">Queue</span><span class="p">()</span>
<span class="n">q</span><span class="o">.</span><span class="n">enqueue</span><span class="p">(</span><span class="s1">'hello'</span><span class="p">)</span>
<span class="n">q</span><span class="o">.</span><span class="n">enqueue</span><span class="p">(</span><span class="s1">'dog'</span><span class="p">)</span>
<span class="n">q</span><span class="o">.</span><span class="n">enqueue</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="n">q</span><span class="o">.</span><span class="n">dequeue</span><span class="p">()</span>
<span class="n">q</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt output_prompt">Out[10]:</div>


<div class="output_text output_subarea output_execute_result">
<pre>&lt;map at 0x7f83a45b56d8&gt;</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Simulation:-Hot-Potato">Simulation: Hot Potato<a class="anchor-link" href="python-data-structures.html#Simulation:-Hot-Potato">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [11]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">pythonds.basic.queue</span> <span class="k">import</span> <span class="n">Queue</span>

<span class="k">def</span> <span class="nf">hotPotato</span><span class="p">(</span><span class="n">namelist</span><span class="p">,</span> <span class="n">num</span><span class="p">):</span>
    <span class="n">simqueue</span> <span class="o">=</span> <span class="n">Queue</span><span class="p">()</span>
    <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">namelist</span><span class="p">:</span>
        <span class="n">simqueue</span><span class="o">.</span><span class="n">enqueue</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
    
    <span class="k">while</span> <span class="n">simqueue</span><span class="o">.</span><span class="n">size</span><span class="p">()</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">num</span><span class="p">):</span>
            <span class="n">simqueue</span><span class="o">.</span><span class="n">enqueue</span><span class="p">(</span><span class="n">simqueue</span><span class="o">.</span><span class="n">dequeue</span><span class="p">())</span>
        <span class="n">simqueue</span><span class="o">.</span><span class="n">dequeue</span><span class="p">()</span>
    <span class="k">return</span> <span class="n">simqueue</span><span class="o">.</span><span class="n">dequeue</span><span class="p">()</span>

<span class="nb">print</span><span class="p">(</span><span class="n">hotPotato</span><span class="p">([</span><span class="s2">"Bill"</span><span class="p">,</span><span class="s2">"David"</span><span class="p">,</span><span class="s2">"Susan"</span><span class="p">,</span><span class="s2">"Jane"</span><span class="p">,</span><span class="s2">"Kent"</span><span class="p">,</span><span class="s2">"Brad"</span><span class="p">],</span><span class="mi">7</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>Susan
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [12]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">HTML</span><span class="p">(</span><span class="s1">'&lt;iframe width="800" height="500" frameborder="0" src="http://pythontutor.com/iframe-embed.html#code=class+Queue%3A%0A++++def+__init__(self)%3A%0A++++++++self.items+%3D+%5B%5D%0A%0A++++def+isEmpty(self)%3A%0A++++++++return+self.items+%3D%3D+%5B%5D%0A%0A++++def+enqueue(self,+item)%3A%0A++++++++self.items.insert(0,item)%0A%0A++++def+dequeue(self)%3A%0A++++++++return+self.items.pop()%0A%0A++++def+size(self)%3A%0A++++++++return+len(self.items)%0A++++%0A++++def+show(self)%3A%0A++++++++return+map(lambda+x%3A+self.dequeue(),+range(self.size()))%0A%0Adef+hotPotato(namelist,+num)%3A%0A++++simqueue+%3D+Queue()%0A++++for+name+in+namelist%3A%0A++++++++simqueue.enqueue(name)%0A%0A++++while+simqueue.size()+</span><span class="si">%3E</span><span class="s1">+1%3A%0A++++++++for+i+in+range(num)%3A%0A++++++++++++simqueue.enqueue(simqueue.dequeue())%0A%0A++++++++simqueue.dequeue()%0A%0A++++return+simqueue.dequeue()%0A%0Aprint(hotPotato(%5B%22Bill%22,%22David%22,%22Susan%22,%22Jane%22,%22Kent%22,%22Brad</span><span class="si">%22%</span><span class="s1">5D,7))%0A&amp;origin=opt-frontend.js&amp;cumulative=false&amp;heapPrimitives=false&amp;drawParentPointers=false&amp;textReferences=false&amp;showOnlyOutputs=false&amp;py=2&amp;rawInputLstJSON=%5B%5D&amp;curInstr=0&amp;codeDivWidth=350&amp;codeDivHeight=400"&gt; &lt;/iframe&gt;'</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt output_prompt">Out[12]:</div>

<div class="output_html rendered_html output_subarea output_execute_result">
<iframe width="800" height="500" frameborder="0" src="http://pythontutor.com/iframe-embed.html#code=class+Queue%3A%0A++++def+__init__(self)%3A%0A++++++++self.items+%3D+%5B%5D%0A%0A++++def+isEmpty(self)%3A%0A++++++++return+self.items+%3D%3D+%5B%5D%0A%0A++++def+enqueue(self,+item)%3A%0A++++++++self.items.insert(0,item)%0A%0A++++def+dequeue(self)%3A%0A++++++++return+self.items.pop()%0A%0A++++def+size(self)%3A%0A++++++++return+len(self.items)%0A++++%0A++++def+show(self)%3A%0A++++++++return+map(lambda+x%3A+self.dequeue(),+range(self.size()))%0A%0Adef+hotPotato(namelist,+num)%3A%0A++++simqueue+%3D+Queue()%0A++++for+name+in+namelist%3A%0A++++++++simqueue.enqueue(name)%0A%0A++++while+simqueue.size()+%3E+1%3A%0A++++++++for+i+in+range(num)%3A%0A++++++++++++simqueue.enqueue(simqueue.dequeue())%0A%0A++++++++simqueue.dequeue()%0A%0A++++return+simqueue.dequeue()%0A%0Aprint(hotPotato(%5B%22Bill%22,%22David%22,%22Susan%22,%22Jane%22,%22Kent%22,%22Brad%22%5D,7))%0A&amp;origin=opt-frontend.js&amp;cumulative=false&amp;heapPrimitives=false&amp;drawParentPointers=false&amp;textReferences=false&amp;showOnlyOutputs=false&amp;py=2&amp;rawInputLstJSON=%5B%5D&amp;curInstr=0&amp;codeDivWidth=350&amp;codeDivHeight=400"> </iframe>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Simulation:-Printing-Tasks">Simulation: Printing Tasks<a class="anchor-link" href="python-data-structures.html#Simulation:-Printing-Tasks">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<ol>
<li>Create a queue of print tasks. Each task will be given a timestamp upon its arrival. The queue is empty to start.</li>
<li>For each second (currentSecond):<ul>
<li>Does a new print task get created? If so, add it to the queue with the currentSecond as the timestamp.</li>
<li>If the printer is not busy and if a task is waiting,<ul>
<li>Remove the next task from the print queue and assign it to the printer.</li>
<li>Subtract the timestamp from the currentSecond to compute the waiting time for that task.</li>
<li>Append the waiting time for that task to a list for later processing.</li>
<li>Based on the number of pages in the print task, figure out how much time will be required.</li>
</ul>
</li>
<li>The printer now does one second of printing if necessary. It also subtracts one second from the time required for that task.</li>
<li>If the task has been completed, in other words the time required has reached zero, the printer is no longer busy.</li>
</ul>
</li>
<li>After the simulation is complete, compute the average waiting time from the list of waiting times generated.</li>
</ol>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [13]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">random</span>

<span class="k">class</span> <span class="nc">Printer</span><span class="p">:</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ppm</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pagerate</span> <span class="o">=</span> <span class="n">ppm</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">currentTask</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">timeRemaining</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="k">def</span> <span class="nf">tick</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">currentTask</span> <span class="o">!=</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">timeRemaining</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">timeRemaining</span> <span class="o">-</span> <span class="mi">1</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">timeRemaining</span> <span class="o">&lt;=</span> <span class="mi">0</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">currentTask</span> <span class="o">=</span> <span class="kc">None</span>

    <span class="k">def</span> <span class="nf">busy</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">currentTask</span> <span class="o">!=</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">False</span>

    <span class="k">def</span> <span class="nf">startNext</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">newtask</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">currentTask</span> <span class="o">=</span> <span class="n">newtask</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">timeRemaining</span> <span class="o">=</span> <span class="n">newtask</span><span class="o">.</span><span class="n">getPages</span><span class="p">()</span> <span class="o">*</span> <span class="mi">60</span><span class="o">/</span><span class="bp">self</span><span class="o">.</span><span class="n">pagerate</span>

<span class="k">class</span> <span class="nc">Task</span><span class="p">:</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">time</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">timestamp</span> <span class="o">=</span> <span class="n">time</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pages</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">randrange</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">21</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">getStamp</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">timestamp</span>

    <span class="k">def</span> <span class="nf">getPages</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">pages</span>

    <span class="k">def</span> <span class="nf">waitTime</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">currenttime</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">currenttime</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">timestamp</span>


<span class="k">def</span> <span class="nf">simulation</span><span class="p">(</span><span class="n">numSeconds</span><span class="p">,</span> <span class="n">pagesPerMinute</span><span class="p">):</span>

    <span class="n">labprinter</span> <span class="o">=</span> <span class="n">Printer</span><span class="p">(</span><span class="n">pagesPerMinute</span><span class="p">)</span>
    <span class="n">printQueue</span> <span class="o">=</span> <span class="n">Queue</span><span class="p">()</span>
    <span class="n">waitingtimes</span> <span class="o">=</span> <span class="p">[]</span>

    <span class="k">for</span> <span class="n">currentSecond</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">numSeconds</span><span class="p">):</span>

      <span class="k">if</span> <span class="n">newPrintTask</span><span class="p">():</span>
         <span class="n">task</span> <span class="o">=</span> <span class="n">Task</span><span class="p">(</span><span class="n">currentSecond</span><span class="p">)</span>
         <span class="n">printQueue</span><span class="o">.</span><span class="n">enqueue</span><span class="p">(</span><span class="n">task</span><span class="p">)</span>

      <span class="k">if</span> <span class="p">(</span><span class="ow">not</span> <span class="n">labprinter</span><span class="o">.</span><span class="n">busy</span><span class="p">())</span> <span class="ow">and</span> <span class="p">(</span><span class="ow">not</span> <span class="n">printQueue</span><span class="o">.</span><span class="n">isEmpty</span><span class="p">()):</span>
        <span class="n">nexttask</span> <span class="o">=</span> <span class="n">printQueue</span><span class="o">.</span><span class="n">dequeue</span><span class="p">()</span>
        <span class="n">waitingtimes</span><span class="o">.</span><span class="n">append</span><span class="p">(</span> <span class="n">nexttask</span><span class="o">.</span><span class="n">waitTime</span><span class="p">(</span><span class="n">currentSecond</span><span class="p">))</span>
        <span class="n">labprinter</span><span class="o">.</span><span class="n">startNext</span><span class="p">(</span><span class="n">nexttask</span><span class="p">)</span>

      <span class="n">labprinter</span><span class="o">.</span><span class="n">tick</span><span class="p">()</span>

    <span class="n">averageWait</span><span class="o">=</span><span class="nb">sum</span><span class="p">(</span><span class="n">waitingtimes</span><span class="p">)</span><span class="o">/</span><span class="nb">len</span><span class="p">(</span><span class="n">waitingtimes</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">"Average Wait </span><span class="si">%6.2f</span><span class="s2"> secs </span><span class="si">%3d</span><span class="s2"> tasks remaining."</span><span class="o">%</span><span class="p">(</span><span class="n">averageWait</span><span class="p">,</span><span class="n">printQueue</span><span class="o">.</span><span class="n">size</span><span class="p">()))</span>

<span class="k">def</span> <span class="nf">newPrintTask</span><span class="p">():</span>
    <span class="n">num</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">randrange</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">181</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">num</span> <span class="o">==</span> <span class="mi">180</span><span class="p">:</span>
        <span class="k">return</span> <span class="kc">True</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="kc">False</span>

<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">):</span>
    <span class="n">simulation</span><span class="p">(</span><span class="mi">3600</span><span class="p">,</span><span class="mi">5</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>Average Wait  31.79 secs   0 tasks remaining.
Average Wait  63.80 secs   0 tasks remaining.
Average Wait  29.67 secs   0 tasks remaining.
Average Wait 161.00 secs   0 tasks remaining.
Average Wait 903.00 secs  11 tasks remaining.
Average Wait 338.50 secs   2 tasks remaining.
Average Wait  82.90 secs   0 tasks remaining.
Average Wait 164.33 secs   1 tasks remaining.
Average Wait  98.30 secs   1 tasks remaining.
Average Wait  50.20 secs   1 tasks remaining.
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="The-Deque-Abstract-Data-Type">The Deque Abstract Data Type<a class="anchor-link" href="python-data-structures.html#The-Deque-Abstract-Data-Type">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<table border="0" class="table" id="tbl-dequeoperations">
<caption><strong>Table 1: Examples of Deque Operations</strong></caption>
<colgroup>
<col width="38%">
<col width="38%">
<col width="24%">
</colgroup>
<thead valign="bottom"><tr class="row-odd">
<th class="head"><strong>Deque Operation</strong></th>
<th class="head"><strong>Deque Contents</strong></th>
<th class="head"><strong>Return Value</strong></th>
</tr></thead>
<tbody valign="top">
<tr class="row-even">
<td><code>d.isEmpty()</code></td>
<td><code>[]</code></td>
<td><code>True</code></td>
</tr>
<tr class="row-odd">
<td><code>d.addRear(4)</code></td>
<td><code>[4]</code></td>
<td> </td>
</tr>
<tr class="row-even">
<td><code>d.addRear('dog')</code></td>
<td><code>['dog',4,]</code></td>
<td> </td>
</tr>
<tr class="row-odd">
<td><code>d.addFront('cat')</code></td>
<td><code>['dog',4,'cat']</code></td>
<td> </td>
</tr>
<tr class="row-even">
<td><code>d.addFront(True)</code></td>
<td><code>['dog',4,'cat',True]</code></td>
<td> </td>
</tr>
<tr class="row-odd">
<td><code>d.size()</code></td>
<td><code>['dog',4,'cat',True]</code></td>
<td><code>4</code></td>
</tr>
<tr class="row-even">
<td><code>d.isEmpty()</code></td>
<td><code>['dog',4,'cat',True]</code></td>
<td><code>False</code></td>
</tr>
<tr class="row-odd">
<td><code>d.addRear(8.4)</code></td>
<td><code>[8.4,'dog',4,'cat',True]</code></td>
<td> </td>
</tr>
<tr class="row-even">
<td><code>d.removeRear()</code></td>
<td><code>['dog',4,'cat',True]</code></td>
<td><code>8.4</code></td>
</tr>
<tr class="row-odd">
<td><code>d.removeFront()</code></td>
<td><code>['dog',4,'cat']</code></td>
<td><code>True</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Implementing-a-Deque-in-Python">Implementing a Deque in Python<a class="anchor-link" href="python-data-structures.html#Implementing-a-Deque-in-Python">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [14]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">class</span> <span class="nc">Deque</span><span class="p">:</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">items</span> <span class="o">=</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="nf">isEmpty</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">items</span> <span class="o">==</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="nf">addFront</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">item</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">items</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">item</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">addRear</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">item</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">items</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="n">item</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">removeFront</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">items</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">removeRear</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">items</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">size</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">items</span><span class="p">)</span>
    
    <span class="k">def</span> <span class="nf">show</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">dequeue</span><span class="p">(),</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">size</span><span class="p">()))</span>
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [15]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">d</span><span class="o">=</span><span class="n">Deque</span><span class="p">()</span>
<span class="nb">print</span><span class="p">(</span><span class="n">d</span><span class="o">.</span><span class="n">isEmpty</span><span class="p">())</span>
<span class="n">d</span><span class="o">.</span><span class="n">addRear</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span>
<span class="n">d</span><span class="o">.</span><span class="n">addRear</span><span class="p">(</span><span class="s1">'dog'</span><span class="p">)</span>
<span class="n">d</span><span class="o">.</span><span class="n">addFront</span><span class="p">(</span><span class="s1">'cat'</span><span class="p">)</span>
<span class="n">d</span><span class="o">.</span><span class="n">addFront</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">d</span><span class="o">.</span><span class="n">size</span><span class="p">())</span>
<span class="nb">print</span><span class="p">(</span><span class="n">d</span><span class="o">.</span><span class="n">isEmpty</span><span class="p">())</span>
<span class="n">d</span><span class="o">.</span><span class="n">addRear</span><span class="p">(</span><span class="mf">8.4</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">d</span><span class="o">.</span><span class="n">removeRear</span><span class="p">())</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>True
4
False
8.4
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Palindrome-Checker(回文检查)">Palindrome-Checker(回文检查)<a class="anchor-link" href="python-data-structures.html#Palindrome-Checker(%E5%9B%9E%E6%96%87%E6%A3%80%E6%9F%A5)">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [16]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">palchecker</span><span class="p">(</span><span class="n">aString</span><span class="p">):</span>
    <span class="n">chardeque</span> <span class="o">=</span> <span class="n">Deque</span><span class="p">()</span>

    <span class="k">for</span> <span class="n">ch</span> <span class="ow">in</span> <span class="n">aString</span><span class="p">:</span>
        <span class="n">chardeque</span><span class="o">.</span><span class="n">addRear</span><span class="p">(</span><span class="n">ch</span><span class="p">)</span>

    <span class="n">stillEqual</span> <span class="o">=</span> <span class="kc">True</span>

    <span class="k">while</span> <span class="n">chardeque</span><span class="o">.</span><span class="n">size</span><span class="p">()</span> <span class="o">&gt;</span> <span class="mi">1</span> <span class="ow">and</span> <span class="n">stillEqual</span><span class="p">:</span>
        <span class="n">first</span> <span class="o">=</span> <span class="n">chardeque</span><span class="o">.</span><span class="n">removeFront</span><span class="p">()</span>
        <span class="n">last</span> <span class="o">=</span> <span class="n">chardeque</span><span class="o">.</span><span class="n">removeRear</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">first</span> <span class="o">!=</span> <span class="n">last</span><span class="p">:</span>
            <span class="n">stillEqual</span> <span class="o">=</span> <span class="kc">False</span>

    <span class="k">return</span> <span class="n">stillEqual</span>

<span class="nb">print</span><span class="p">(</span><span class="n">palchecker</span><span class="p">(</span><span class="s2">"lsdkjfskf"</span><span class="p">))</span>
<span class="nb">print</span><span class="p">(</span><span class="n">palchecker</span><span class="p">(</span><span class="s2">"radar"</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>False
True
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="The-Unordered-List-Abstract-Data-Type">The Unordered List Abstract Data Type<a class="anchor-link" href="python-data-structures.html#The-Unordered-List-Abstract-Data-Type">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<ul>
<li>List() creates a new list that is empty. It needs no parameters and returns an empty list.</li>
<li>add(item) adds a new item to the list. It needs the item and returns nothing. Assume the item is not already in the list.</li>
<li>remove(item) removes the item from the list. It needs the item and modifies the list. Assume the item is present in the list.</li>
<li>search(item) searches for the item in the list. It needs the item and returns a boolean value.</li>
<li>isEmpty() tests to see whether the list is empty. It needs no parameters and returns a boolean value.</li>
<li>size() returns the number of items in the list. It needs no parameters and returns an integer.</li>
<li>append(item) adds a new item to the end of the list making it the last item in the collection. It needs the item and returns nothing. Assume the item is not already in the list.</li>
<li>index(item) returns the position of item in the list. It needs the item and returns the index. Assume the item is in the list.</li>
<li>insert(pos,item) adds a new item to the list at position pos. It needs the item and returns nothing. Assume the item is not already in the list and there are enough existing items to have position pos.</li>
<li>pop() removes and returns the last item in the list. It needs nothing and returns an item. Assume the list has at least one item.</li>
<li>pop(pos) removes and returns the item at position pos. It needs the position and returns the item. Assume the item is in the list.</li>
</ul>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Implementing-an-Unordered-List:-Linked-Lists">Implementing an Unordered List: Linked Lists<a class="anchor-link" href="python-data-structures.html#Implementing-an-Unordered-List:-Linked-Lists">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [17]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">class</span> <span class="nc">Node</span><span class="p">:</span>
    <span class="sd">"""</span>
<span class="sd">    The basic building block for the linked list implementation</span>
<span class="sd">    is the node. Each node object must hold at least two pieces</span>
<span class="sd">    of information. First, the node must contain the list item </span>
<span class="sd">    itself. We will call this the data field of the node. </span>
<span class="sd">    """</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">initdata</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">data</span> <span class="o">=</span> <span class="n">initdata</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">next</span> <span class="o">=</span> <span class="kc">None</span>

    <span class="k">def</span> <span class="nf">getData</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span>

    <span class="k">def</span> <span class="nf">getNext</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">next</span>

    <span class="k">def</span> <span class="nf">setData</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">newdata</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">data</span> <span class="o">=</span> <span class="n">newdata</span>

    <span class="k">def</span> <span class="nf">setNext</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">newnext</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">next</span> <span class="o">=</span> <span class="n">newnext</span>


<span class="k">class</span> <span class="nc">UnorderedList</span><span class="p">:</span>
    <span class="sd">"""</span>
<span class="sd">    The unordered list will be built from a collection of nodes, </span>
<span class="sd">    each linked to the next by explicit references. As long as we </span>
<span class="sd">    know where to find the first node (containing the first item), </span>
<span class="sd">    each item after that can be found by successively following </span>
<span class="sd">    the next links. </span>
<span class="sd">    """</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">head</span> <span class="o">=</span> <span class="kc">None</span>

    <span class="k">def</span> <span class="nf">isEmpty</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">head</span> <span class="o">==</span> <span class="kc">None</span>

    <span class="k">def</span> <span class="nf">add</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">item</span><span class="p">):</span>
        <span class="n">temp</span> <span class="o">=</span> <span class="n">Node</span><span class="p">(</span><span class="n">item</span><span class="p">)</span>
        <span class="n">temp</span><span class="o">.</span><span class="n">setNext</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">head</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">head</span> <span class="o">=</span> <span class="n">temp</span>

    <span class="k">def</span> <span class="nf">size</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">current</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">head</span>
        <span class="n">count</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">while</span> <span class="n">current</span> <span class="o">!=</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">count</span> <span class="o">=</span> <span class="n">count</span> <span class="o">+</span> <span class="mi">1</span>
            <span class="n">current</span> <span class="o">=</span> <span class="n">current</span><span class="o">.</span><span class="n">getNext</span><span class="p">()</span>

        <span class="k">return</span> <span class="n">count</span>

    <span class="k">def</span> <span class="nf">search</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">item</span><span class="p">):</span>
        <span class="n">current</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">head</span>
        <span class="n">found</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="k">while</span> <span class="n">current</span> <span class="o">!=</span> <span class="kc">None</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">found</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">current</span><span class="o">.</span><span class="n">getData</span><span class="p">()</span> <span class="o">==</span> <span class="n">item</span><span class="p">:</span>
                <span class="n">found</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">current</span> <span class="o">=</span> <span class="n">current</span><span class="o">.</span><span class="n">getNext</span><span class="p">()</span>

        <span class="k">return</span> <span class="n">found</span>

    <span class="k">def</span> <span class="nf">remove</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">item</span><span class="p">):</span>
        <span class="n">current</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">head</span>
        <span class="n">previous</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="n">found</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="k">while</span> <span class="ow">not</span> <span class="n">found</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">current</span><span class="o">.</span><span class="n">getData</span><span class="p">()</span> <span class="o">==</span> <span class="n">item</span><span class="p">:</span>
                <span class="n">found</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">previous</span> <span class="o">=</span> <span class="n">current</span>
                <span class="n">current</span> <span class="o">=</span> <span class="n">current</span><span class="o">.</span><span class="n">getNext</span><span class="p">()</span>

        <span class="k">if</span> <span class="n">previous</span> <span class="o">==</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">head</span> <span class="o">=</span> <span class="n">current</span><span class="o">.</span><span class="n">getNext</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">previous</span><span class="o">.</span><span class="n">setNext</span><span class="p">(</span><span class="n">current</span><span class="o">.</span><span class="n">getNext</span><span class="p">())</span>
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [18]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">mylist</span> <span class="o">=</span> <span class="n">UnorderedList</span><span class="p">()</span>

<span class="n">mylist</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="mi">31</span><span class="p">)</span>
<span class="n">mylist</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="mi">77</span><span class="p">)</span>
<span class="n">mylist</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="mi">17</span><span class="p">)</span>
<span class="n">mylist</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="mi">93</span><span class="p">)</span>
<span class="n">mylist</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="mi">26</span><span class="p">)</span>
<span class="n">mylist</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="mi">54</span><span class="p">)</span>

<span class="nb">print</span><span class="p">(</span><span class="n">mylist</span><span class="o">.</span><span class="n">size</span><span class="p">())</span>
<span class="nb">print</span><span class="p">(</span><span class="n">mylist</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="mi">93</span><span class="p">))</span>
<span class="nb">print</span><span class="p">(</span><span class="n">mylist</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="mi">100</span><span class="p">))</span>

<span class="n">mylist</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="mi">100</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">mylist</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="mi">100</span><span class="p">))</span>
<span class="nb">print</span><span class="p">(</span><span class="n">mylist</span><span class="o">.</span><span class="n">size</span><span class="p">())</span>

<span class="n">mylist</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="mi">54</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">mylist</span><span class="o">.</span><span class="n">size</span><span class="p">())</span>
<span class="n">mylist</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="mi">93</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">mylist</span><span class="o">.</span><span class="n">size</span><span class="p">())</span>
<span class="n">mylist</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="mi">31</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">mylist</span><span class="o">.</span><span class="n">size</span><span class="p">())</span>
<span class="nb">print</span><span class="p">(</span><span class="n">mylist</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="mi">93</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>6
True
False
True
7
6
5
4
False
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="The-Ordered-List-Abstract-Data-Type">The Ordered List Abstract Data Type<a class="anchor-link" href="python-data-structures.html#The-Ordered-List-Abstract-Data-Type">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>The structure of an ordered list is a collection of items where each item holds a relative position that is based upon some underlying characteristic of the item. The ordering is typically either ascending or descending and we assume that list items have a meaningful comparison operation that is already defined. Many of the ordered list operations are the same as those of the unordered list.</p>
<ul>
<li>OrderedList() creates a new ordered list that is empty. It needs no parameters and returns an empty list.</li>
<li>add(item) adds a new item to the list making sure that the order is preserved. It needs the item and returns nothing. Assume the item is not already in the list.</li>
<li>remove(item) removes the item from the list. It needs the item and modifies the list. Assume the item is present in the list.</li>
<li>search(item) searches for the item in the list. It needs the item and returns a boolean value.</li>
<li>isEmpty() tests to see whether the list is empty. It needs no parameters and returns a boolean value.</li>
<li>size() returns the number of items in the list. It needs no parameters and returns an integer.</li>
<li>index(item) returns the position of item in the list. It needs the item and returns the index. Assume the item is in the list.</li>
<li>pop() removes and returns the last item in the list. It needs nothing and returns an item. Assume the list has at least one item.</li>
<li>pop(pos) removes and returns the item at position pos. It needs the position and returns the item. Assume the item is in the list.</li>
</ul>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Implementing-an-Ordered-List">Implementing an Ordered List<a class="anchor-link" href="python-data-structures.html#Implementing-an-Ordered-List">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [19]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">class</span> <span class="nc">OrderedList</span><span class="p">:</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">head</span> <span class="o">=</span> <span class="kc">None</span>

    <span class="k">def</span> <span class="nf">search</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">item</span><span class="p">):</span>
        <span class="n">current</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">head</span>
        <span class="n">found</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="n">stop</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="k">while</span> <span class="n">current</span> <span class="o">!=</span> <span class="kc">None</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">found</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">stop</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">current</span><span class="o">.</span><span class="n">getData</span><span class="p">()</span> <span class="o">==</span> <span class="n">item</span><span class="p">:</span>
                <span class="n">found</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">current</span><span class="o">.</span><span class="n">getData</span><span class="p">()</span> <span class="o">&gt;</span> <span class="n">item</span><span class="p">:</span>
                    <span class="n">stop</span> <span class="o">=</span> <span class="kc">True</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">current</span> <span class="o">=</span> <span class="n">current</span><span class="o">.</span><span class="n">getNext</span><span class="p">()</span>

        <span class="k">return</span> <span class="n">found</span>

    <span class="k">def</span> <span class="nf">add</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">item</span><span class="p">):</span>
        <span class="n">current</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">head</span>
        <span class="n">previous</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="n">stop</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="k">while</span> <span class="n">current</span> <span class="o">!=</span> <span class="kc">None</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">stop</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">current</span><span class="o">.</span><span class="n">getData</span><span class="p">()</span> <span class="o">&gt;</span> <span class="n">item</span><span class="p">:</span>
                <span class="n">stop</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">previous</span> <span class="o">=</span> <span class="n">current</span>
                <span class="n">current</span> <span class="o">=</span> <span class="n">current</span><span class="o">.</span><span class="n">getNext</span><span class="p">()</span>

        <span class="n">temp</span> <span class="o">=</span> <span class="n">Node</span><span class="p">(</span><span class="n">item</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">previous</span> <span class="o">==</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">temp</span><span class="o">.</span><span class="n">setNext</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">head</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">head</span> <span class="o">=</span> <span class="n">temp</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">temp</span><span class="o">.</span><span class="n">setNext</span><span class="p">(</span><span class="n">current</span><span class="p">)</span>
            <span class="n">previous</span><span class="o">.</span><span class="n">setNext</span><span class="p">(</span><span class="n">temp</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">isEmpty</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">head</span> <span class="o">==</span> <span class="kc">None</span>

    <span class="k">def</span> <span class="nf">size</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">current</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">head</span>
        <span class="n">count</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">while</span> <span class="n">current</span> <span class="o">!=</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">count</span> <span class="o">=</span> <span class="n">count</span> <span class="o">+</span> <span class="mi">1</span>
            <span class="n">current</span> <span class="o">=</span> <span class="n">current</span><span class="o">.</span><span class="n">getNext</span><span class="p">()</span>

        <span class="k">return</span> <span class="n">count</span>
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [20]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">mylist</span> <span class="o">=</span> <span class="n">OrderedList</span><span class="p">()</span>
<span class="n">mylist</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="mi">31</span><span class="p">)</span>
<span class="n">mylist</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="mi">77</span><span class="p">)</span>
<span class="n">mylist</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="mi">17</span><span class="p">)</span>
<span class="n">mylist</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="mi">93</span><span class="p">)</span>
<span class="n">mylist</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="mi">26</span><span class="p">)</span>
<span class="n">mylist</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="mi">54</span><span class="p">)</span>

<span class="nb">print</span><span class="p">(</span><span class="n">mylist</span><span class="o">.</span><span class="n">size</span><span class="p">())</span>
<span class="nb">print</span><span class="p">(</span><span class="n">mylist</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="mi">93</span><span class="p">))</span>
<span class="nb">print</span><span class="p">(</span><span class="n">mylist</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="mi">100</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>6
True
False
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="The-Three-Laws-of-Recursion">The Three Laws of Recursion<a class="anchor-link" href="python-data-structures.html#The-Three-Laws-of-Recursion">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<ol>
<li>A recursive algorithm must have a base case.</li>
<li>A recursive algorithm must change its state and move toward the base case.</li>
<li>A recursive algorithm must call itself, recursively.</li>
</ol>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [21]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">listsum</span><span class="p">(</span><span class="n">numList</span><span class="p">):</span>
    <span class="n">theSum</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">numList</span><span class="p">:</span>
        <span class="n">theSum</span> <span class="o">=</span> <span class="n">theSum</span> <span class="o">+</span> <span class="n">i</span>
    <span class="k">return</span> <span class="n">theSum</span>

<span class="nb">print</span><span class="p">(</span><span class="n">listsum</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">7</span><span class="p">,</span><span class="mi">9</span><span class="p">]))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>25
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [22]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">listsum</span><span class="p">(</span><span class="n">numList</span><span class="p">):</span>
   <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">numList</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">numList</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
   <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">numList</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="n">listsum</span><span class="p">(</span><span class="n">numList</span><span class="p">[</span><span class="mi">1</span><span class="p">:])</span>

<span class="nb">print</span><span class="p">(</span><span class="n">listsum</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">7</span><span class="p">,</span><span class="mi">9</span><span class="p">]))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>25
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Converting-an-Integer-to-a-String-in-Any-Base">Converting an Integer to a String in Any Base<a class="anchor-link" href="python-data-structures.html#Converting-an-Integer-to-a-String-in-Any-Base">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [23]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">toStr</span><span class="p">(</span><span class="n">n</span><span class="p">,</span><span class="n">base</span><span class="p">):</span>
   <span class="n">convertString</span> <span class="o">=</span> <span class="s2">"0123456789ABCDEF"</span>
   <span class="k">if</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="n">base</span><span class="p">:</span>
      <span class="k">return</span> <span class="n">convertString</span><span class="p">[</span><span class="n">n</span><span class="p">]</span>
   <span class="k">else</span><span class="p">:</span>
      <span class="k">return</span> <span class="n">toStr</span><span class="p">(</span><span class="n">n</span><span class="o">//</span><span class="n">base</span><span class="p">,</span><span class="n">base</span><span class="p">)</span> <span class="o">+</span> <span class="n">convertString</span><span class="p">[</span><span class="n">n</span><span class="o">%</span><span class="k">base</span>]

<span class="nb">print</span><span class="p">(</span><span class="n">toStr</span><span class="p">(</span><span class="mi">1453</span><span class="p">,</span><span class="mi">16</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>5AD
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [24]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">reverse</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>
    <span class="sd">"""</span>
<span class="sd">    a function that takes a string as a parameter and returns</span>
<span class="sd">    a new string that is the reverse of the old string.</span>
<span class="sd">    """</span>
    <span class="k">if</span> <span class="n">s</span> <span class="o">==</span> <span class="s1">''</span><span class="p">:</span> <span class="k">return</span> <span class="n">s</span>
    <span class="k">else</span><span class="p">:</span> <span class="k">return</span> <span class="n">reverse</span><span class="p">(</span><span class="n">s</span><span class="p">[</span><span class="mi">1</span><span class="p">:])</span> <span class="o">+</span> <span class="n">s</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [25]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="n">reverse</span><span class="p">(</span><span class="s2">"follow"</span><span class="p">))</span>
<span class="nb">print</span><span class="p">(</span><span class="n">reverse</span><span class="p">(</span><span class="s2">"a +"</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>wollof
+ a
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [26]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">string</span>

<span class="k">def</span> <span class="nf">removeWhite</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>
    <span class="n">exclude</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">string</span><span class="o">.</span><span class="n">punctuation</span><span class="o">+</span><span class="s1">' '</span><span class="p">)</span>
    <span class="n">s</span> <span class="o">=</span> <span class="s1">''</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">ch</span> <span class="k">for</span> <span class="n">ch</span> <span class="ow">in</span> <span class="n">s</span> <span class="k">if</span> <span class="n">ch</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">exclude</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">s</span>

<span class="k">def</span> <span class="nf">isPal</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>
    <span class="sd">"""</span>
<span class="sd">    Write a function that takes a string as a parameter and</span>
<span class="sd">    returns True if the string is a palindrome, False otherwise. </span>
<span class="sd">    """</span>
    <span class="n">s</span> <span class="o">=</span> <span class="n">removeWhite</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
    <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mi">2</span> <span class="ow">or</span> <span class="n">s</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="n">s</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="ow">and</span> <span class="n">isPal</span><span class="p">(</span><span class="n">s</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [27]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="n">isPal</span><span class="p">(</span><span class="n">removeWhite</span><span class="p">(</span><span class="s2">"x"</span><span class="p">)))</span>
<span class="nb">print</span><span class="p">(</span><span class="n">isPal</span><span class="p">(</span><span class="n">removeWhite</span><span class="p">(</span><span class="s2">"radar"</span><span class="p">)))</span>
<span class="nb">print</span><span class="p">(</span><span class="n">isPal</span><span class="p">(</span><span class="n">removeWhite</span><span class="p">(</span><span class="s2">"hello"</span><span class="p">)))</span>
<span class="nb">print</span><span class="p">(</span><span class="n">isPal</span><span class="p">(</span><span class="n">removeWhite</span><span class="p">(</span><span class="s2">""</span><span class="p">)))</span>
<span class="nb">print</span><span class="p">(</span><span class="n">isPal</span><span class="p">(</span><span class="n">removeWhite</span><span class="p">(</span><span class="s2">"hannah"</span><span class="p">)))</span>
<span class="nb">print</span><span class="p">(</span><span class="n">isPal</span><span class="p">(</span><span class="n">removeWhite</span><span class="p">(</span><span class="s2">"madam i'm adam"</span><span class="p">)))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>True
True
False
True
True
True
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Stack-Frames:-Implementing-Recursion">Stack Frames: Implementing Recursion<a class="anchor-link" href="python-data-structures.html#Stack-Frames:-Implementing-Recursion">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [28]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">rStack</span> <span class="o">=</span> <span class="n">Stack</span><span class="p">()</span>

<span class="k">def</span> <span class="nf">toStr</span><span class="p">(</span><span class="n">n</span><span class="p">,</span><span class="n">base</span><span class="p">):</span>
    <span class="n">convertString</span> <span class="o">=</span> <span class="s2">"0123456789ABCDEF"</span>
    <span class="k">while</span> <span class="n">n</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="n">base</span><span class="p">:</span>
            <span class="n">rStack</span><span class="o">.</span><span class="n">push</span><span class="p">(</span><span class="n">convertString</span><span class="p">[</span><span class="n">n</span><span class="p">])</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">rStack</span><span class="o">.</span><span class="n">push</span><span class="p">(</span><span class="n">convertString</span><span class="p">[</span><span class="n">n</span> <span class="o">%</span> <span class="n">base</span><span class="p">])</span>
        <span class="n">n</span> <span class="o">=</span> <span class="n">n</span> <span class="o">//</span> <span class="n">base</span>
    <span class="n">res</span> <span class="o">=</span> <span class="s2">""</span>
    <span class="k">while</span> <span class="ow">not</span> <span class="n">rStack</span><span class="o">.</span><span class="n">isEmpty</span><span class="p">():</span>
        <span class="n">res</span> <span class="o">=</span> <span class="n">res</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">rStack</span><span class="o">.</span><span class="n">pop</span><span class="p">())</span>
    <span class="k">return</span> <span class="n">res</span>

<span class="nb">print</span><span class="p">(</span><span class="n">toStr</span><span class="p">(</span><span class="mi">1453</span><span class="p">,</span><span class="mi">16</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>5AD
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [29]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">HTML</span><span class="p">(</span><span class="s1">'&lt;iframe width="1000" height="500" frameborder="0" src="http://pythontutor.com/iframe-embed.html#code=class+Stack%3A%0A++++def+__init__(self)%3A%0A++++++++self.items+%3D+%5B%5D%0A%0A++++def+isEmpty(self)%3A%0A++++++++return+self.items+%3D%3D+%5B%5D%0A%0A++++def+push(self,+item)%3A%0A++++++++self.items.append(item)%0A%0A++++def+pop(self)%3A%0A++++++++return+self.items.pop()%0A%0A++++def+peek(self)%3A%0A++++++++return+self.items%5Blen(self.items)-1%5D%0A%0A++++def+size(self)%3A%0A++++++++return+len(self.items)%0A++++%0ArStack+%3D+Stack()%0A%0Adef+toStr(n,base)%3A%0A++++convertString+%3D+%220123456789ABCDEF</span><span class="si">%22%</span><span class="s1">0A++++while+n+</span><span class="si">%3E</span><span class="s1">+0%3A%0A++++++++if+n+%3C+base%3A%0A++++++++++++rStack.push(convertString%5Bn%5D)%0A++++++++else%3A%0A++++++++++++rStack.push(convertString%5Bn+%25+base%5D)%0A++++++++n+%3D+n+//+base%0A++++res+%3D+</span><span class="si">%22%</span><span class="s1">22%0A++++while+not+rStack.isEmpty()%3A%0A++++++++res+%3D+res+%2B+str(rStack.pop())%0A++++return+res%0A%0Aprint(toStr(1453,16))&amp;origin=opt-frontend.js&amp;cumulative=false&amp;heapPrimitives=false&amp;drawParentPointers=false&amp;textReferences=false&amp;showOnlyOutputs=false&amp;py=2&amp;rawInputLstJSON=%5B%5D&amp;curInstr=29&amp;codeDivWidth=350&amp;codeDivHeight=400"&gt; &lt;/iframe&gt;'</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt output_prompt">Out[29]:</div>

<div class="output_html rendered_html output_subarea output_execute_result">
<iframe width="1000" height="500" frameborder="0" src="http://pythontutor.com/iframe-embed.html#code=class+Stack%3A%0A++++def+__init__(self)%3A%0A++++++++self.items+%3D+%5B%5D%0A%0A++++def+isEmpty(self)%3A%0A++++++++return+self.items+%3D%3D+%5B%5D%0A%0A++++def+push(self,+item)%3A%0A++++++++self.items.append(item)%0A%0A++++def+pop(self)%3A%0A++++++++return+self.items.pop()%0A%0A++++def+peek(self)%3A%0A++++++++return+self.items%5Blen(self.items)-1%5D%0A%0A++++def+size(self)%3A%0A++++++++return+len(self.items)%0A++++%0ArStack+%3D+Stack()%0A%0Adef+toStr(n,base)%3A%0A++++convertString+%3D+%220123456789ABCDEF%22%0A++++while+n+%3E+0%3A%0A++++++++if+n+%3C+base%3A%0A++++++++++++rStack.push(convertString%5Bn%5D)%0A++++++++else%3A%0A++++++++++++rStack.push(convertString%5Bn+%25+base%5D)%0A++++++++n+%3D+n+//+base%0A++++res+%3D+%22%22%0A++++while+not+rStack.isEmpty()%3A%0A++++++++res+%3D+res+%2B+str(rStack.pop())%0A++++return+res%0A%0Aprint(toStr(1453,16))&amp;origin=opt-frontend.js&amp;cumulative=false&amp;heapPrimitives=false&amp;drawParentPointers=false&amp;textReferences=false&amp;showOnlyOutputs=false&amp;py=2&amp;rawInputLstJSON=%5B%5D&amp;curInstr=29&amp;codeDivWidth=350&amp;codeDivHeight=400"> </iframe>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Tower-of-Hanoi">Tower of Hanoi<a class="anchor-link" href="python-data-structures.html#Tower-of-Hanoi">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>The number of moves required to correctly move a tower of $n$ disks is $2^n-1$.Here is a high-level outline of how to move a tower from the starting pole, to the goal pole, using an intermediate pole:</p>
<ol>
<li>Move a tower of height-1 to an intermediate pole, using the final pole. 用C把高度height-1的塔从A移动到B</li>
<li>Move the remaining disk to the final pole. 把A剩下的碟子移动到C</li>
<li>Move the tower of height-1 from the intermediate pole to the final pole using the original pole. 用A把高度height-1的塔从B移动到C</li>
</ol>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [30]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">moveTower</span><span class="p">(</span><span class="n">height</span><span class="p">,</span><span class="n">fromPole</span><span class="p">,</span> <span class="n">toPole</span><span class="p">,</span> <span class="n">withPole</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">height</span> <span class="o">&gt;=</span> <span class="mi">1</span><span class="p">:</span>
        <span class="n">moveTower</span><span class="p">(</span><span class="n">height</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="n">fromPole</span><span class="p">,</span><span class="n">withPole</span><span class="p">,</span><span class="n">toPole</span><span class="p">)</span>
        <span class="n">moveDisk</span><span class="p">(</span><span class="n">fromPole</span><span class="p">,</span><span class="n">toPole</span><span class="p">)</span>
        <span class="n">moveTower</span><span class="p">(</span><span class="n">height</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="n">withPole</span><span class="p">,</span><span class="n">toPole</span><span class="p">,</span><span class="n">fromPole</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">moveDisk</span><span class="p">(</span><span class="n">fp</span><span class="p">,</span><span class="n">tp</span><span class="p">):</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">"moving disk from"</span><span class="p">,</span><span class="n">fp</span><span class="p">,</span><span class="s2">"to"</span><span class="p">,</span><span class="n">tp</span><span class="p">)</span>

<span class="n">moveTower</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="s2">"A"</span><span class="p">,</span><span class="s2">"B"</span><span class="p">,</span><span class="s2">"C"</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>moving disk from A to B
moving disk from A to C
moving disk from B to C
moving disk from A to B
moving disk from C to A
moving disk from C to B
moving disk from A to B
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [31]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">HTML</span><span class="p">(</span><span class="s1">'&lt;iframe width="1000" height="500" frameborder="0" src="http://pythontutor.com/iframe-embed.html#code=def+moveTower(height,fromPole,+toPole,+withPole)%3A%0A++++if+height+</span><span class="si">%3E</span><span class="s1">%3D+1%3A%0A++++++++moveTower(height-1,fromPole,withPole,toPole)%0A++++++++moveDisk(fromPole,toPole)%0A++++++++moveTower(height-1,withPole,toPole,fromPole)%0A%0Adef+moveDisk(fp,tp)%3A%0A++++print+%22moving+disk+from%22,fp,%22to%22,tp%0A%0AmoveTower(3,%22A%22,%22B%22,%22C%22)&amp;origin=opt-frontend.js&amp;cumulative=false&amp;heapPrimitives=false&amp;drawParentPointers=false&amp;textReferences=false&amp;showOnlyOutputs=false&amp;py=2&amp;rawInputLstJSON=%5B%5D&amp;curInstr=80&amp;codeDivWidth=350&amp;codeDivHeight=400"&gt; &lt;/iframe&gt;'</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt output_prompt">Out[31]:</div>

<div class="output_html rendered_html output_subarea output_execute_result">
<iframe width="1000" height="500" frameborder="0" src="http://pythontutor.com/iframe-embed.html#code=def+moveTower(height,fromPole,+toPole,+withPole)%3A%0A++++if+height+%3E%3D+1%3A%0A++++++++moveTower(height-1,fromPole,withPole,toPole)%0A++++++++moveDisk(fromPole,toPole)%0A++++++++moveTower(height-1,withPole,toPole,fromPole)%0A%0Adef+moveDisk(fp,tp)%3A%0A++++print+%22moving+disk+from%22,fp,%22to%22,tp%0A%0AmoveTower(3,%22A%22,%22B%22,%22C%22)&amp;origin=opt-frontend.js&amp;cumulative=false&amp;heapPrimitives=false&amp;drawParentPointers=false&amp;textReferences=false&amp;showOnlyOutputs=false&amp;py=2&amp;rawInputLstJSON=%5B%5D&amp;curInstr=80&amp;codeDivWidth=350&amp;codeDivHeight=400"> </iframe>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [32]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">moveTower</span><span class="p">(</span><span class="n">height</span><span class="p">,</span><span class="n">fromPole</span><span class="p">,</span> <span class="n">toPole</span><span class="p">,</span> <span class="n">withPole</span><span class="p">):</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">"moveTower:"</span><span class="p">,</span> <span class="n">height</span><span class="p">,</span><span class="n">fromPole</span><span class="p">,</span> <span class="n">toPole</span><span class="p">,</span> <span class="n">withPole</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">height</span> <span class="o">&gt;=</span> <span class="mi">1</span><span class="p">:</span>
        <span class="n">moveTower</span><span class="p">(</span><span class="n">height</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="n">fromPole</span><span class="p">,</span><span class="n">withPole</span><span class="p">,</span><span class="n">toPole</span><span class="p">)</span>
        <span class="n">moveDisk</span><span class="p">(</span><span class="n">fromPole</span><span class="p">,</span><span class="n">toPole</span><span class="p">)</span>
        <span class="n">moveTower</span><span class="p">(</span><span class="n">height</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="n">withPole</span><span class="p">,</span><span class="n">toPole</span><span class="p">,</span><span class="n">fromPole</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">moveDisk</span><span class="p">(</span><span class="n">fp</span><span class="p">,</span><span class="n">tp</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">fromPole</span><span class="p">[</span><span class="mi">1</span><span class="p">]:</span>
        <span class="n">disk</span> <span class="o">=</span> <span class="n">fromPole</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">"moving "</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">disk</span><span class="p">)</span> <span class="o">+</span> <span class="s2">" from "</span> <span class="o">+</span> <span class="n">fromPole</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="s2">" to "</span> <span class="o">+</span> <span class="n">toPole</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
        <span class="n">toPole</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">disk</span><span class="p">)</span>
    
<span class="n">fromPole</span> <span class="o">=</span> <span class="p">(</span><span class="s2">"A"</span><span class="p">,</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">1</span><span class="p">])</span>
<span class="n">toPole</span> <span class="o">=</span> <span class="p">(</span><span class="s2">"C"</span><span class="p">,</span> <span class="p">[])</span>
<span class="n">withPole</span> <span class="o">=</span> <span class="p">(</span><span class="s2">"B"</span><span class="p">,</span> <span class="p">[])</span>

<span class="n">moveTower</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">fromPole</span><span class="p">[</span><span class="mi">1</span><span class="p">]),</span> <span class="n">fromPole</span><span class="p">,</span> <span class="n">toPole</span><span class="p">,</span> <span class="n">withPole</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">fromPole</span><span class="p">,</span> <span class="n">withPole</span><span class="p">,</span> <span class="n">toPole</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>moveTower: 3 ('A', [3, 2, 1]) ('C', []) ('B', [])
moveTower: 2 ('A', [3, 2, 1]) ('B', []) ('C', [])
moveTower: 1 ('A', [3, 2, 1]) ('C', []) ('B', [])
moveTower: 0 ('A', [3, 2, 1]) ('B', []) ('C', [])
moving 1 from A to C
moveTower: 0 ('B', []) ('C', [1]) ('A', [3, 2])
moving 2 from A to C
moveTower: 1 ('C', [1, 2]) ('B', []) ('A', [3])
moveTower: 0 ('C', [1, 2]) ('A', [3]) ('B', [])
moving 3 from A to C
moveTower: 0 ('A', []) ('B', []) ('C', [1, 2, 3])
moveTower: 2 ('B', []) ('C', [1, 2, 3]) ('A', [])
moveTower: 1 ('B', []) ('A', []) ('C', [1, 2, 3])
moveTower: 0 ('B', []) ('C', [1, 2, 3]) ('A', [])
moveTower: 0 ('C', [1, 2, 3]) ('A', []) ('B', [])
moveTower: 1 ('A', []) ('C', [1, 2, 3]) ('B', [])
moveTower: 0 ('A', []) ('B', []) ('C', [1, 2, 3])
moveTower: 0 ('B', []) ('C', [1, 2, 3]) ('A', [])
('A', []) ('B', []) ('C', [1, 2, 3])
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Exploring-a-Maze">Exploring a Maze<a class="anchor-link" href="python-data-structures.html#Exploring-a-Maze">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [33]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">turtle</span>
<span class="kn">from</span> <span class="nn">__future__</span> <span class="k">import</span> <span class="n">division</span>

<span class="n">PART_OF_PATH</span> <span class="o">=</span> <span class="s1">'O'</span>
<span class="n">TRIED</span> <span class="o">=</span> <span class="s1">'.'</span>
<span class="n">OBSTACLE</span> <span class="o">=</span> <span class="s1">'+'</span>
<span class="n">DEAD_END</span> <span class="o">=</span> <span class="s1">'-'</span>

<span class="k">class</span> <span class="nc">Maze</span><span class="p">:</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">mazeFileName</span><span class="p">):</span>
        <span class="n">rowsInMaze</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="n">columnsInMaze</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">mazelist</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">mazeFile</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">mazeFileName</span><span class="p">,</span><span class="s1">'r'</span><span class="p">)</span>
        <span class="n">rowsInMaze</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">mazeFile</span><span class="p">:</span>
            <span class="n">rowList</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="n">col</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="k">for</span> <span class="n">ch</span> <span class="ow">in</span> <span class="n">line</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]:</span>
                <span class="n">rowList</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">ch</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">ch</span> <span class="o">==</span> <span class="s1">'S'</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">startRow</span> <span class="o">=</span> <span class="n">rowsInMaze</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">startCol</span> <span class="o">=</span> <span class="n">col</span>
                <span class="n">col</span> <span class="o">=</span> <span class="n">col</span> <span class="o">+</span> <span class="mi">1</span>
            <span class="n">rowsInMaze</span> <span class="o">=</span> <span class="n">rowsInMaze</span> <span class="o">+</span> <span class="mi">1</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">mazelist</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">rowList</span><span class="p">)</span>
            <span class="n">columnsInMaze</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">rowList</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">rowsInMaze</span> <span class="o">=</span> <span class="n">rowsInMaze</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">columnsInMaze</span> <span class="o">=</span> <span class="n">columnsInMaze</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">xTranslate</span> <span class="o">=</span> <span class="o">-</span><span class="n">columnsInMaze</span><span class="o">/</span><span class="mi">2</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">yTranslate</span> <span class="o">=</span> <span class="n">rowsInMaze</span><span class="o">/</span><span class="mi">2</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">t</span> <span class="o">=</span> <span class="n">turtle</span><span class="o">.</span><span class="n">Turtle</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">t</span><span class="o">.</span><span class="n">shape</span><span class="p">(</span><span class="s1">'turtle'</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">wn</span> <span class="o">=</span> <span class="n">turtle</span><span class="o">.</span><span class="n">Screen</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">wn</span><span class="o">.</span><span class="n">setworldcoordinates</span><span class="p">(</span><span class="o">-</span><span class="p">(</span><span class="n">columnsInMaze</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span><span class="o">/</span><span class="mi">2</span><span class="o">-.</span><span class="mi">5</span><span class="p">,</span><span class="o">-</span><span class="p">(</span><span class="n">rowsInMaze</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span><span class="o">/</span><span class="mi">2</span><span class="o">-.</span><span class="mi">5</span><span class="p">,(</span><span class="n">columnsInMaze</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span><span class="o">/</span><span class="mi">2</span><span class="o">+.</span><span class="mi">5</span><span class="p">,(</span><span class="n">rowsInMaze</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span><span class="o">/</span><span class="mi">2</span><span class="o">+.</span><span class="mi">5</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">drawMaze</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">t</span><span class="o">.</span><span class="n">speed</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">y</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rowsInMaze</span><span class="p">):</span>
            <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">columnsInMaze</span><span class="p">):</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">mazelist</span><span class="p">[</span><span class="n">y</span><span class="p">][</span><span class="n">x</span><span class="p">]</span> <span class="o">==</span> <span class="n">OBSTACLE</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">drawCenteredBox</span><span class="p">(</span><span class="n">x</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">xTranslate</span><span class="p">,</span><span class="o">-</span><span class="n">y</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">yTranslate</span><span class="p">,</span><span class="s1">'orange'</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">t</span><span class="o">.</span><span class="n">color</span><span class="p">(</span><span class="s1">'black'</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">t</span><span class="o">.</span><span class="n">fillcolor</span><span class="p">(</span><span class="s1">'blue'</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">drawCenteredBox</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">,</span><span class="n">color</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">t</span><span class="o">.</span><span class="n">up</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">t</span><span class="o">.</span><span class="n">goto</span><span class="p">(</span><span class="n">x</span><span class="o">-.</span><span class="mi">5</span><span class="p">,</span><span class="n">y</span><span class="o">-.</span><span class="mi">5</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">t</span><span class="o">.</span><span class="n">color</span><span class="p">(</span><span class="n">color</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">t</span><span class="o">.</span><span class="n">fillcolor</span><span class="p">(</span><span class="n">color</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">t</span><span class="o">.</span><span class="n">setheading</span><span class="p">(</span><span class="mi">90</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">t</span><span class="o">.</span><span class="n">down</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">t</span><span class="o">.</span><span class="n">begin_fill</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">4</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">t</span><span class="o">.</span><span class="n">forward</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">t</span><span class="o">.</span><span class="n">right</span><span class="p">(</span><span class="mi">90</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">t</span><span class="o">.</span><span class="n">end_fill</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">moveTurtle</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">t</span><span class="o">.</span><span class="n">up</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">t</span><span class="o">.</span><span class="n">setheading</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">t</span><span class="o">.</span><span class="n">towards</span><span class="p">(</span><span class="n">x</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">xTranslate</span><span class="p">,</span><span class="o">-</span><span class="n">y</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">yTranslate</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">t</span><span class="o">.</span><span class="n">goto</span><span class="p">(</span><span class="n">x</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">xTranslate</span><span class="p">,</span><span class="o">-</span><span class="n">y</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">yTranslate</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">dropBreadcrumb</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">color</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">t</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span><span class="n">color</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">updatePosition</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">row</span><span class="p">,</span><span class="n">col</span><span class="p">,</span><span class="n">val</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">val</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">mazelist</span><span class="p">[</span><span class="n">row</span><span class="p">][</span><span class="n">col</span><span class="p">]</span> <span class="o">=</span> <span class="n">val</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">moveTurtle</span><span class="p">(</span><span class="n">col</span><span class="p">,</span><span class="n">row</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">val</span> <span class="o">==</span> <span class="n">PART_OF_PATH</span><span class="p">:</span>
            <span class="n">color</span> <span class="o">=</span> <span class="s1">'green'</span>
        <span class="k">elif</span> <span class="n">val</span> <span class="o">==</span> <span class="n">OBSTACLE</span><span class="p">:</span>
            <span class="n">color</span> <span class="o">=</span> <span class="s1">'red'</span>
        <span class="k">elif</span> <span class="n">val</span> <span class="o">==</span> <span class="n">TRIED</span><span class="p">:</span>
            <span class="n">color</span> <span class="o">=</span> <span class="s1">'black'</span>
        <span class="k">elif</span> <span class="n">val</span> <span class="o">==</span> <span class="n">DEAD_END</span><span class="p">:</span>
            <span class="n">color</span> <span class="o">=</span> <span class="s1">'red'</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">color</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="k">if</span> <span class="n">color</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">dropBreadcrumb</span><span class="p">(</span><span class="n">color</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">isExit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">row</span><span class="p">,</span><span class="n">col</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">(</span><span class="n">row</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">or</span>
                <span class="n">row</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">rowsInMaze</span><span class="o">-</span><span class="mi">1</span> <span class="ow">or</span>
                <span class="n">col</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">or</span>
                <span class="n">col</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">columnsInMaze</span><span class="o">-</span><span class="mi">1</span> <span class="p">)</span>

    <span class="k">def</span> <span class="nf">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">idx</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">mazelist</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span>


<span class="k">def</span> <span class="nf">searchFrom</span><span class="p">(</span><span class="n">maze</span><span class="p">,</span> <span class="n">startRow</span><span class="p">,</span> <span class="n">startColumn</span><span class="p">):</span>
    <span class="c1"># try each of four directions from this point until we find a way out.</span>
    <span class="c1"># base Case return values:</span>
    <span class="c1">#  1. We have run into an obstacle, return false</span>
    <span class="n">maze</span><span class="o">.</span><span class="n">updatePosition</span><span class="p">(</span><span class="n">startRow</span><span class="p">,</span> <span class="n">startColumn</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">maze</span><span class="p">[</span><span class="n">startRow</span><span class="p">][</span><span class="n">startColumn</span><span class="p">]</span> <span class="o">==</span> <span class="n">OBSTACLE</span> <span class="p">:</span>
        <span class="k">return</span> <span class="kc">False</span>
    <span class="c1">#  2. We have found a square that has already been explored</span>
    <span class="k">if</span> <span class="n">maze</span><span class="p">[</span><span class="n">startRow</span><span class="p">][</span><span class="n">startColumn</span><span class="p">]</span> <span class="o">==</span> <span class="n">TRIED</span> <span class="ow">or</span> <span class="n">maze</span><span class="p">[</span><span class="n">startRow</span><span class="p">][</span><span class="n">startColumn</span><span class="p">]</span> <span class="o">==</span> <span class="n">DEAD_END</span><span class="p">:</span>
        <span class="k">return</span> <span class="kc">False</span>
    <span class="c1"># 3. We have found an outside edge not occupied by an obstacle</span>
    <span class="k">if</span> <span class="n">maze</span><span class="o">.</span><span class="n">isExit</span><span class="p">(</span><span class="n">startRow</span><span class="p">,</span><span class="n">startColumn</span><span class="p">):</span>
        <span class="n">maze</span><span class="o">.</span><span class="n">updatePosition</span><span class="p">(</span><span class="n">startRow</span><span class="p">,</span> <span class="n">startColumn</span><span class="p">,</span> <span class="n">PART_OF_PATH</span><span class="p">)</span>
        <span class="k">return</span> <span class="kc">True</span>
    <span class="n">maze</span><span class="o">.</span><span class="n">updatePosition</span><span class="p">(</span><span class="n">startRow</span><span class="p">,</span> <span class="n">startColumn</span><span class="p">,</span> <span class="n">TRIED</span><span class="p">)</span>
    <span class="c1"># Otherwise, use logical short circuiting to try each direction</span>
    <span class="c1"># in turn (if needed)</span>
    <span class="n">found</span> <span class="o">=</span> <span class="n">searchFrom</span><span class="p">(</span><span class="n">maze</span><span class="p">,</span> <span class="n">startRow</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="n">startColumn</span><span class="p">)</span> <span class="ow">or</span> \
            <span class="n">searchFrom</span><span class="p">(</span><span class="n">maze</span><span class="p">,</span> <span class="n">startRow</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span> <span class="n">startColumn</span><span class="p">)</span> <span class="ow">or</span> \
            <span class="n">searchFrom</span><span class="p">(</span><span class="n">maze</span><span class="p">,</span> <span class="n">startRow</span><span class="p">,</span> <span class="n">startColumn</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="ow">or</span> \
            <span class="n">searchFrom</span><span class="p">(</span><span class="n">maze</span><span class="p">,</span> <span class="n">startRow</span><span class="p">,</span> <span class="n">startColumn</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">found</span><span class="p">:</span>
        <span class="n">maze</span><span class="o">.</span><span class="n">updatePosition</span><span class="p">(</span><span class="n">startRow</span><span class="p">,</span> <span class="n">startColumn</span><span class="p">,</span> <span class="n">PART_OF_PATH</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">maze</span><span class="o">.</span><span class="n">updatePosition</span><span class="p">(</span><span class="n">startRow</span><span class="p">,</span> <span class="n">startColumn</span><span class="p">,</span> <span class="n">DEAD_END</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">found</span>


<span class="c1"># myMaze = Maze('maze2.txt')</span>
<span class="c1"># myMaze.drawMaze()</span>
<span class="c1"># myMaze.updatePosition(myMaze.startRow,myMaze.startCol)</span>

<span class="c1"># searchFrom(myMaze, myMaze.startRow, myMaze.startCol)</span>
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Dynamic-Programming">Dynamic Programming<a class="anchor-link" href="python-data-structures.html#Dynamic-Programming">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [34]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">recDC</span><span class="p">(</span><span class="n">coinValueList</span><span class="p">,</span><span class="n">change</span><span class="p">,</span><span class="n">knownResults</span><span class="p">):</span>
   <span class="n">minCoins</span> <span class="o">=</span> <span class="n">change</span>
   <span class="k">if</span> <span class="n">change</span> <span class="ow">in</span> <span class="n">coinValueList</span><span class="p">:</span>
      <span class="n">knownResults</span><span class="p">[</span><span class="n">change</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>
      <span class="k">return</span> <span class="mi">1</span>
   <span class="k">elif</span> <span class="n">knownResults</span><span class="p">[</span><span class="n">change</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
      <span class="k">return</span> <span class="n">knownResults</span><span class="p">[</span><span class="n">change</span><span class="p">]</span>
   <span class="k">else</span><span class="p">:</span>
       <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="p">[</span><span class="n">c</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">coinValueList</span> <span class="k">if</span> <span class="n">c</span> <span class="o">&lt;=</span> <span class="n">change</span><span class="p">]:</span>
         <span class="n">numCoins</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">+</span> <span class="n">recDC</span><span class="p">(</span><span class="n">coinValueList</span><span class="p">,</span> <span class="n">change</span><span class="o">-</span><span class="n">i</span><span class="p">,</span>
                              <span class="n">knownResults</span><span class="p">)</span>
         <span class="k">if</span> <span class="n">numCoins</span> <span class="o">&lt;</span> <span class="n">minCoins</span><span class="p">:</span>
            <span class="n">minCoins</span> <span class="o">=</span> <span class="n">numCoins</span>
            <span class="n">knownResults</span><span class="p">[</span><span class="n">change</span><span class="p">]</span> <span class="o">=</span> <span class="n">minCoins</span>
   <span class="k">return</span> <span class="n">minCoins</span>

<span class="nb">print</span><span class="p">(</span><span class="n">recDC</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">10</span><span class="p">,</span><span class="mi">25</span><span class="p">],</span><span class="mi">63</span><span class="p">,[</span><span class="mi">0</span><span class="p">]</span><span class="o">*</span><span class="mi">64</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>6
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [35]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">dpMakeChange</span><span class="p">(</span><span class="n">coinValueList</span><span class="p">,</span><span class="n">change</span><span class="p">,</span><span class="n">minCoins</span><span class="p">,</span><span class="n">coinsUsed</span><span class="p">):</span>
   <span class="k">for</span> <span class="n">cents</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">change</span><span class="o">+</span><span class="mi">1</span><span class="p">):</span>
      <span class="n">coinCount</span> <span class="o">=</span> <span class="n">cents</span>
      <span class="n">newCoin</span> <span class="o">=</span> <span class="mi">1</span>
      <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="p">[</span><span class="n">c</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">coinValueList</span> <span class="k">if</span> <span class="n">c</span> <span class="o">&lt;=</span> <span class="n">cents</span><span class="p">]:</span>
            <span class="k">if</span> <span class="n">minCoins</span><span class="p">[</span><span class="n">cents</span><span class="o">-</span><span class="n">j</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span> <span class="o">&lt;</span> <span class="n">coinCount</span><span class="p">:</span>
               <span class="n">coinCount</span> <span class="o">=</span> <span class="n">minCoins</span><span class="p">[</span><span class="n">cents</span><span class="o">-</span><span class="n">j</span><span class="p">]</span><span class="o">+</span><span class="mi">1</span>
               <span class="n">newCoin</span> <span class="o">=</span> <span class="n">j</span>
      <span class="n">minCoins</span><span class="p">[</span><span class="n">cents</span><span class="p">]</span> <span class="o">=</span> <span class="n">coinCount</span>
      <span class="n">coinsUsed</span><span class="p">[</span><span class="n">cents</span><span class="p">]</span> <span class="o">=</span> <span class="n">newCoin</span>
   <span class="k">return</span> <span class="n">minCoins</span><span class="p">[</span><span class="n">change</span><span class="p">]</span>

<span class="k">def</span> <span class="nf">printCoins</span><span class="p">(</span><span class="n">coinsUsed</span><span class="p">,</span><span class="n">change</span><span class="p">):</span>
   <span class="n">coin</span> <span class="o">=</span> <span class="n">change</span>
   <span class="k">while</span> <span class="n">coin</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
      <span class="n">thisCoin</span> <span class="o">=</span> <span class="n">coinsUsed</span><span class="p">[</span><span class="n">coin</span><span class="p">]</span>
      <span class="nb">print</span><span class="p">(</span><span class="n">thisCoin</span><span class="p">)</span>
      <span class="n">coin</span> <span class="o">=</span> <span class="n">coin</span> <span class="o">-</span> <span class="n">thisCoin</span>

<span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
    <span class="n">amnt</span> <span class="o">=</span> <span class="mi">63</span>
    <span class="n">clist</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">10</span><span class="p">,</span><span class="mi">21</span><span class="p">,</span><span class="mi">25</span><span class="p">]</span>
    <span class="n">coinsUsed</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">*</span><span class="p">(</span><span class="n">amnt</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span>
    <span class="n">coinCount</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">*</span><span class="p">(</span><span class="n">amnt</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span>

    <span class="nb">print</span><span class="p">(</span><span class="s2">"Making change for"</span><span class="p">,</span><span class="n">amnt</span><span class="p">,</span><span class="s2">"requires"</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">dpMakeChange</span><span class="p">(</span><span class="n">clist</span><span class="p">,</span><span class="n">amnt</span><span class="p">,</span><span class="n">coinCount</span><span class="p">,</span><span class="n">coinsUsed</span><span class="p">),</span><span class="s2">"coins"</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">"They are:"</span><span class="p">)</span>
    <span class="n">printCoins</span><span class="p">(</span><span class="n">coinsUsed</span><span class="p">,</span><span class="n">amnt</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">"The used list is as follows:"</span><span class="p">)</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">coinsUsed</span><span class="p">)</span>

<span class="n">main</span><span class="p">()</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>Making change for 63 requires
3 coins
They are:
21
21
21
The used list is as follows:
[1, 1, 1, 1, 1, 5, 1, 1, 1, 1, 10, 1, 1, 1, 1, 5, 1, 1, 1, 1, 10, 21, 1, 1, 1, 25, 1, 1, 1, 1, 5, 10, 1, 1, 1, 10, 1, 1, 1, 1, 5, 10, 21, 1, 1, 10, 21, 1, 1, 1, 25, 1, 10, 1, 1, 5, 10, 1, 1, 1, 10, 1, 10, 21]
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<ol>
<li>All recursive algorithms must have a base case.</li>
<li>A recursive algorithm must change its state and make progress toward the base case.</li>
<li>A recursive algorithm must call itself (recursively).</li>
<li>Recursion can take the place of iteration in some cases.</li>
<li>Recursive algorithms often map very naturally to a formal expression of the problem you are trying to solve.</li>
<li>Recursion is not always the answer. Sometimes a recursive solution may be more computationally expensive than an alternative algorithm</li>
</ol>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="The-Sequential-Search">The Sequential Search<a class="anchor-link" href="python-data-structures.html#The-Sequential-Search">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<table border="0" class="table" id="tbl-seqsearchtable">
<caption><strong>Table 1: Comparisons Used in a Sequential Search of an Unordered List</strong></caption>
<colgroup>
<col width="21%">
<col width="27%">
<col width="27%">
<col width="25%">
</colgroup>
<thead valign="bottom"><tr class="row-odd">
<th class="head"><strong>Case</strong></th>
<th class="head"><strong>Best Case</strong></th>
<th class="head"><strong>Worst Case</strong></th>
<th class="head"><strong>Average Case</strong></th>
</tr></thead>
<tbody valign="top">
<tr class="row-even">
<td>item is present</td>
<td><span class="math"><span class="MathJax_Preview"></span><span class="MathJax" id="MathJax-Element-5-Frame" role="textbox" aria-readonly="true"><nobr><span class="math" id="MathJax-Span-18" style="width: 0.647em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.53em; height: 0px; font-size: 122%;"><span style="position: absolute; clip: rect(1.876em 1000.003em 2.872em -0.465em); top: -2.69em; left: 0.003em;"><span class="mrow" id="MathJax-Span-19"><span class="mn" id="MathJax-Span-20" style="font-family: MathJax_Main;">1</span></span><span style="display: inline-block; width: 0px; height: 2.696em;"></span></span></span><span style="border-left-width: 0.004em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 0.932em; vertical-align: -0.068em;"></span></span></nobr></span><script type="math/tex" id="MathJax-Element-5">1</script></span></td>
<td><span class="math"><span class="MathJax_Preview"></span><span class="MathJax" id="MathJax-Element-6-Frame" role="textbox" aria-readonly="true"><nobr><span class="math" id="MathJax-Span-21" style="width: 0.823em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.647em; height: 0px; font-size: 122%;"><span style="position: absolute; clip: rect(1.818em 1000.003em 2.638em -0.524em); top: -2.456em; left: 0.003em;"><span class="mrow" id="MathJax-Span-22"><span class="mi" id="MathJax-Span-23" style="font-family: MathJax_Math; font-style: italic;">n</span></span><span style="display: inline-block; width: 0px; height: 2.462em;"></span></span></span><span style="border-left-width: 0.004em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 0.718em; vertical-align: -0.068em;"></span></span></nobr></span><script type="math/tex" id="MathJax-Element-6">n</script></span></td>
<td><span class="math"><span class="MathJax_Preview"></span><span class="MathJax" id="MathJax-Element-7-Frame" role="textbox" aria-readonly="true"><nobr><span class="math" id="MathJax-Span-24" style="width: 0.94em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.764em; height: 0px; font-size: 122%;"><span style="position: absolute; clip: rect(1.174em 1000.003em 2.696em -0.524em); top: -2.105em; left: 0.003em;"><span class="mrow" id="MathJax-Span-25"><span class="mfrac" id="MathJax-Span-26"><span style="display: inline-block; position: relative; width: 0.53em; height: 0px; margin-right: 0.12em; margin-left: 0.12em;"><span style="position: absolute; clip: rect(1.759em 1000.003em 2.403em -0.524em); top: -2.632em; left: 50%; margin-left: -0.173em;"><span class="mi" id="MathJax-Span-27" style="font-size: 70.7%; font-family: MathJax_Math; font-style: italic;">n</span><span style="display: inline-block; width: 0px; height: 2.228em;"></span></span><span style="position: absolute; clip: rect(1.642em 1000.003em 2.462em -0.465em); top: -1.871em; left: 50%; margin-left: -0.173em;"><span class="mn" id="MathJax-Span-28" style="font-size: 70.7%; font-family: MathJax_Main;">2</span><span style="display: inline-block; width: 0px; height: 2.286em;"></span></span><span style="position: absolute; clip: rect(0.823em 1000.003em 1.232em -0.524em); top: -1.285em; left: 0.003em;"><span style="border-left-width: 0.53em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 1.25px; vertical-align: 0.003em;"></span><span style="display: inline-block; width: 0px; height: 1.057em;"></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 2.111em;"></span></span></span><span style="border-left-width: 0.004em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 1.504em; vertical-align: -0.568em;"></span></span></nobr></span><script type="math/tex" id="MathJax-Element-7">\frac{n}{2}</script></span></td>
</tr>
<tr class="row-odd">
<td>item is not present</td>
<td><span class="math"><span class="MathJax_Preview"></span><span class="MathJax" id="MathJax-Element-8-Frame" role="textbox" aria-readonly="true"><nobr><span class="math" id="MathJax-Span-29" style="width: 0.823em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.647em; height: 0px; font-size: 122%;"><span style="position: absolute; clip: rect(1.818em 1000.003em 2.638em -0.524em); top: -2.456em; left: 0.003em;"><span class="mrow" id="MathJax-Span-30"><span class="mi" id="MathJax-Span-31" style="font-family: MathJax_Math; font-style: italic;">n</span></span><span style="display: inline-block; width: 0px; height: 2.462em;"></span></span></span><span style="border-left-width: 0.004em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 0.718em; vertical-align: -0.068em;"></span></span></nobr></span><script type="math/tex" id="MathJax-Element-8">n</script></span></td>
<td><span class="math"><span class="MathJax_Preview"></span><span class="MathJax" id="MathJax-Element-9-Frame" role="textbox" aria-readonly="true"><nobr><span class="math" id="MathJax-Span-32" style="width: 0.823em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.647em; height: 0px; font-size: 122%;"><span style="position: absolute; clip: rect(1.818em 1000.003em 2.638em -0.524em); top: -2.456em; left: 0.003em;"><span class="mrow" id="MathJax-Span-33"><span class="mi" id="MathJax-Span-34" style="font-family: MathJax_Math; font-style: italic;">n</span></span><span style="display: inline-block; width: 0px; height: 2.462em;"></span></span></span><span style="border-left-width: 0.004em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 0.718em; vertical-align: -0.068em;"></span></span></nobr></span><script type="math/tex" id="MathJax-Element-9">n</script></span></td>
<td><span class="math"><span class="MathJax_Preview"></span><span class="MathJax" id="MathJax-Element-10-Frame" role="textbox" aria-readonly="true"><nobr><span class="math" id="MathJax-Span-35" style="width: 0.823em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.647em; height: 0px; font-size: 122%;"><span style="position: absolute; clip: rect(1.818em 1000.003em 2.638em -0.524em); top: -2.456em; left: 0.003em;"><span class="mrow" id="MathJax-Span-36"><span class="mi" id="MathJax-Span-37" style="font-family: MathJax_Math; font-style: italic;">n</span></span><span style="display: inline-block; width: 0px; height: 2.462em;"></span></span></span><span style="border-left-width: 0.004em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 0.718em; vertical-align: -0.068em;"></span></span></nobr></span><script type="math/tex" id="MathJax-Element-10">n</script></span></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [36]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">sequentialSearch</span><span class="p">(</span><span class="n">alist</span><span class="p">,</span> <span class="n">item</span><span class="p">):</span>
    <span class="n">pos</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="n">found</span> <span class="o">=</span> <span class="kc">False</span>

    <span class="k">while</span> <span class="n">pos</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">alist</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">found</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">alist</span><span class="p">[</span><span class="n">pos</span><span class="p">]</span> <span class="o">==</span> <span class="n">item</span><span class="p">:</span>
            <span class="n">found</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">pos</span> <span class="o">=</span> <span class="n">pos</span> <span class="o">+</span> <span class="mi">1</span>

    <span class="k">return</span> <span class="n">found</span>

<span class="n">testlist</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">32</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">17</span><span class="p">,</span> <span class="mi">19</span><span class="p">,</span> <span class="mi">42</span><span class="p">,</span> <span class="mi">13</span><span class="p">,</span> <span class="mi">0</span><span class="p">]</span>
<span class="nb">print</span><span class="p">(</span><span class="n">sequentialSearch</span><span class="p">(</span><span class="n">testlist</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span>
<span class="nb">print</span><span class="p">(</span><span class="n">sequentialSearch</span><span class="p">(</span><span class="n">testlist</span><span class="p">,</span> <span class="mi">13</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>False
True
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<table border="0" class="table" id="tbl-seqsearchtable2">
<caption><strong>Table 2: Comparisons Used in Sequential Search of an Ordered List</strong></caption>
<colgroup>
<col width="25%">
<col width="22%">
<col width="22%">
<col width="30%">
</colgroup>
<thead valign="bottom"><tr class="row-odd">
<th class="head"> </th>
<th class="head"><strong>Best Case</strong></th>
<th class="head"><strong>Worst Case</strong></th>
<th class="head"><strong>Average Case</strong></th>
</tr></thead>
<tbody valign="top">
<tr class="row-even">
<td>item is present</td>
<td><span class="math"><span class="MathJax_Preview"></span><span class="MathJax" id="MathJax-Element-13-Frame" role="textbox" aria-readonly="true"><nobr><span class="math" id="MathJax-Span-49" style="width: 0.647em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.53em; height: 0px; font-size: 122%;"><span style="position: absolute; clip: rect(1.876em 1000.003em 2.872em -0.465em); top: -2.69em; left: 0.003em;"><span class="mrow" id="MathJax-Span-50"><span class="mn" id="MathJax-Span-51" style="font-family: MathJax_Main;">1</span></span><span style="display: inline-block; width: 0px; height: 2.696em;"></span></span></span><span style="border-left-width: 0.004em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 0.932em; vertical-align: -0.068em;"></span></span></nobr></span><script type="math/tex" id="MathJax-Element-13">1</script></span></td>
<td><span class="math"><span class="MathJax_Preview"></span><span class="MathJax" id="MathJax-Element-14-Frame" role="textbox" aria-readonly="true"><nobr><span class="math" id="MathJax-Span-52" style="width: 0.823em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.647em; height: 0px; font-size: 122%;"><span style="position: absolute; clip: rect(1.818em 1000.003em 2.638em -0.524em); top: -2.456em; left: 0.003em;"><span class="mrow" id="MathJax-Span-53"><span class="mi" id="MathJax-Span-54" style="font-family: MathJax_Math; font-style: italic;">n</span></span><span style="display: inline-block; width: 0px; height: 2.462em;"></span></span></span><span style="border-left-width: 0.004em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 0.718em; vertical-align: -0.068em;"></span></span></nobr></span><script type="math/tex" id="MathJax-Element-14">n</script></span></td>
<td><span class="math"><span class="MathJax_Preview"></span><span class="MathJax" id="MathJax-Element-15-Frame" role="textbox" aria-readonly="true"><nobr><span class="math" id="MathJax-Span-55" style="width: 0.94em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.764em; height: 0px; font-size: 122%;"><span style="position: absolute; clip: rect(1.174em 1000.003em 2.696em -0.524em); top: -2.105em; left: 0.003em;"><span class="mrow" id="MathJax-Span-56"><span class="mfrac" id="MathJax-Span-57"><span style="display: inline-block; position: relative; width: 0.53em; height: 0px; margin-right: 0.12em; margin-left: 0.12em;"><span style="position: absolute; clip: rect(1.759em 1000.003em 2.403em -0.524em); top: -2.632em; left: 50%; margin-left: -0.173em;"><span class="mi" id="MathJax-Span-58" style="font-size: 70.7%; font-family: MathJax_Math; font-style: italic;">n</span><span style="display: inline-block; width: 0px; height: 2.228em;"></span></span><span style="position: absolute; clip: rect(1.642em 1000.003em 2.462em -0.465em); top: -1.871em; left: 50%; margin-left: -0.173em;"><span class="mn" id="MathJax-Span-59" style="font-size: 70.7%; font-family: MathJax_Main;">2</span><span style="display: inline-block; width: 0px; height: 2.286em;"></span></span><span style="position: absolute; clip: rect(0.823em 1000.003em 1.232em -0.524em); top: -1.285em; left: 0.003em;"><span style="border-left-width: 0.53em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 1.25px; vertical-align: 0.003em;"></span><span style="display: inline-block; width: 0px; height: 1.057em;"></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 2.111em;"></span></span></span><span style="border-left-width: 0.004em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 1.504em; vertical-align: -0.568em;"></span></span></nobr></span><script type="math/tex" id="MathJax-Element-15">\frac{n}{2}</script></span></td>
</tr>
<tr class="row-odd">
<td>item not present</td>
<td><span class="math"><span class="MathJax_Preview"></span><span class="MathJax" id="MathJax-Element-16-Frame" role="textbox" aria-readonly="true"><nobr><span class="math" id="MathJax-Span-60" style="width: 0.647em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.53em; height: 0px; font-size: 122%;"><span style="position: absolute; clip: rect(1.876em 1000.003em 2.872em -0.465em); top: -2.69em; left: 0.003em;"><span class="mrow" id="MathJax-Span-61"><span class="mn" id="MathJax-Span-62" style="font-family: MathJax_Main;">1</span></span><span style="display: inline-block; width: 0px; height: 2.696em;"></span></span></span><span style="border-left-width: 0.004em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 0.932em; vertical-align: -0.068em;"></span></span></nobr></span><script type="math/tex" id="MathJax-Element-16">1</script></span></td>
<td><span class="math"><span class="MathJax_Preview"></span><span class="MathJax" id="MathJax-Element-17-Frame" role="textbox" aria-readonly="true"><nobr><span class="math" id="MathJax-Span-63" style="width: 0.823em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.647em; height: 0px; font-size: 122%;"><span style="position: absolute; clip: rect(1.818em 1000.003em 2.638em -0.524em); top: -2.456em; left: 0.003em;"><span class="mrow" id="MathJax-Span-64"><span class="mi" id="MathJax-Span-65" style="font-family: MathJax_Math; font-style: italic;">n</span></span><span style="display: inline-block; width: 0px; height: 2.462em;"></span></span></span><span style="border-left-width: 0.004em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 0.718em; vertical-align: -0.068em;"></span></span></nobr></span><script type="math/tex" id="MathJax-Element-17">n</script></span></td>
<td><span class="math"><span class="MathJax_Preview"></span><span class="MathJax" id="MathJax-Element-18-Frame" role="textbox" aria-readonly="true"><nobr><span class="math" id="MathJax-Span-66" style="width: 0.94em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.764em; height: 0px; font-size: 122%;"><span style="position: absolute; clip: rect(1.174em 1000.003em 2.696em -0.524em); top: -2.105em; left: 0.003em;"><span class="mrow" id="MathJax-Span-67"><span class="mfrac" id="MathJax-Span-68"><span style="display: inline-block; position: relative; width: 0.53em; height: 0px; margin-right: 0.12em; margin-left: 0.12em;"><span style="position: absolute; clip: rect(1.759em 1000.003em 2.403em -0.524em); top: -2.632em; left: 50%; margin-left: -0.173em;"><span class="mi" id="MathJax-Span-69" style="font-size: 70.7%; font-family: MathJax_Math; font-style: italic;">n</span><span style="display: inline-block; width: 0px; height: 2.228em;"></span></span><span style="position: absolute; clip: rect(1.642em 1000.003em 2.462em -0.465em); top: -1.871em; left: 50%; margin-left: -0.173em;"><span class="mn" id="MathJax-Span-70" style="font-size: 70.7%; font-family: MathJax_Main;">2</span><span style="display: inline-block; width: 0px; height: 2.286em;"></span></span><span style="position: absolute; clip: rect(0.823em 1000.003em 1.232em -0.524em); top: -1.285em; left: 0.003em;"><span style="border-left-width: 0.53em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 1.25px; vertical-align: 0.003em;"></span><span style="display: inline-block; width: 0px; height: 1.057em;"></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 2.111em;"></span></span></span><span style="border-left-width: 0.004em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 1.504em; vertical-align: -0.568em;"></span></span></nobr></span><script type="math/tex" id="MathJax-Element-18">\frac{n}{2}</script></span></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [37]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">orderedSequentialSearch</span><span class="p">(</span><span class="n">alist</span><span class="p">,</span> <span class="n">item</span><span class="p">):</span>
    <span class="n">pos</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="n">found</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="n">stop</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="k">while</span> <span class="n">pos</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">alist</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">found</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">stop</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">alist</span><span class="p">[</span><span class="n">pos</span><span class="p">]</span> <span class="o">==</span> <span class="n">item</span><span class="p">:</span>
            <span class="n">found</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">alist</span><span class="p">[</span><span class="n">pos</span><span class="p">]</span> <span class="o">&gt;</span> <span class="n">item</span><span class="p">:</span>
                <span class="n">stop</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">pos</span> <span class="o">=</span> <span class="n">pos</span><span class="o">+</span><span class="mi">1</span>

    <span class="k">return</span> <span class="n">found</span>

<span class="n">testlist</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">13</span><span class="p">,</span> <span class="mi">17</span><span class="p">,</span> <span class="mi">19</span><span class="p">,</span> <span class="mi">32</span><span class="p">,</span> <span class="mi">42</span><span class="p">,]</span>
<span class="nb">print</span><span class="p">(</span><span class="n">orderedSequentialSearch</span><span class="p">(</span><span class="n">testlist</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span>
<span class="nb">print</span><span class="p">(</span><span class="n">orderedSequentialSearch</span><span class="p">(</span><span class="n">testlist</span><span class="p">,</span> <span class="mi">13</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>False
True
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="The-Binary-Search">The Binary Search<a class="anchor-link" href="python-data-structures.html#The-Binary-Search">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<table border="0" class="table" id="tbl-binaryanalysis">
<caption><strong>Table 3: Tabular Analysis for a Binary Search</strong></caption>
<colgroup>
<col width="39%">
<col width="61%">
</colgroup>
<thead valign="bottom"><tr class="row-odd">
<th class="head"><strong>Comparisons</strong></th>
<th class="head"><strong>Approximate Number of Items Left</strong></th>
</tr></thead>
<tbody valign="top">
<tr class="row-even">
<td>1</td>
<td><span class="math"><span class="MathJax_Preview"></span><span class="MathJax" id="MathJax-Element-6-Frame" role="textbox" aria-readonly="true"><nobr><span class="math" id="MathJax-Span-26" style="width: 0.94em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.764em; height: 0px; font-size: 122%;"><span style="position: absolute; clip: rect(1.174em 1000.003em 2.696em -0.524em); top: -2.105em; left: 0.003em;"><span class="mrow" id="MathJax-Span-27"><span class="mfrac" id="MathJax-Span-28"><span style="display: inline-block; position: relative; width: 0.53em; height: 0px; margin-right: 0.12em; margin-left: 0.12em;"><span style="position: absolute; clip: rect(1.759em 1000.003em 2.403em -0.524em); top: -2.632em; left: 50%; margin-left: -0.173em;"><span class="mi" id="MathJax-Span-29" style="font-size: 70.7%; font-family: MathJax_Math; font-style: italic;">n</span><span style="display: inline-block; width: 0px; height: 2.228em;"></span></span><span style="position: absolute; clip: rect(1.642em 1000.003em 2.462em -0.465em); top: -1.871em; left: 50%; margin-left: -0.173em;"><span class="mn" id="MathJax-Span-30" style="font-size: 70.7%; font-family: MathJax_Main;">2</span><span style="display: inline-block; width: 0px; height: 2.286em;"></span></span><span style="position: absolute; clip: rect(0.823em 1000.003em 1.232em -0.524em); top: -1.285em; left: 0.003em;"><span style="border-left-width: 0.53em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 1.25px; vertical-align: 0.003em;"></span><span style="display: inline-block; width: 0px; height: 1.057em;"></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 2.111em;"></span></span></span><span style="border-left-width: 0.004em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 1.504em; vertical-align: -0.568em;"></span></span></nobr></span><script type="math/tex" id="MathJax-Element-6">\frac {n}{2}</script></span></td>
</tr>
<tr class="row-odd">
<td>2</td>
<td><span class="math"><span class="MathJax_Preview"></span><span class="MathJax" id="MathJax-Element-7-Frame" role="textbox" aria-readonly="true"><nobr><span class="math" id="MathJax-Span-31" style="width: 0.94em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.764em; height: 0px; font-size: 122%;"><span style="position: absolute; clip: rect(1.174em 1000.003em 2.696em -0.524em); top: -2.105em; left: 0.003em;"><span class="mrow" id="MathJax-Span-32"><span class="mfrac" id="MathJax-Span-33"><span style="display: inline-block; position: relative; width: 0.53em; height: 0px; margin-right: 0.12em; margin-left: 0.12em;"><span style="position: absolute; clip: rect(1.759em 1000.003em 2.403em -0.524em); top: -2.632em; left: 50%; margin-left: -0.173em;"><span class="mi" id="MathJax-Span-34" style="font-size: 70.7%; font-family: MathJax_Math; font-style: italic;">n</span><span style="display: inline-block; width: 0px; height: 2.228em;"></span></span><span style="position: absolute; clip: rect(1.642em 1000.003em 2.462em -0.524em); top: -1.871em; left: 50%; margin-left: -0.173em;"><span class="mn" id="MathJax-Span-35" style="font-size: 70.7%; font-family: MathJax_Main;">4</span><span style="display: inline-block; width: 0px; height: 2.286em;"></span></span><span style="position: absolute; clip: rect(0.823em 1000.003em 1.232em -0.524em); top: -1.285em; left: 0.003em;"><span style="border-left-width: 0.53em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 1.25px; vertical-align: 0.003em;"></span><span style="display: inline-block; width: 0px; height: 1.057em;"></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 2.111em;"></span></span></span><span style="border-left-width: 0.004em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 1.504em; vertical-align: -0.568em;"></span></span></nobr></span><script type="math/tex" id="MathJax-Element-7">\frac {n}{4}</script></span></td>
</tr>
<tr class="row-even">
<td>3</td>
<td><span class="math"><span class="MathJax_Preview"></span><span class="MathJax" id="MathJax-Element-8-Frame" role="textbox" aria-readonly="true"><nobr><span class="math" id="MathJax-Span-36" style="width: 0.94em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.764em; height: 0px; font-size: 122%;"><span style="position: absolute; clip: rect(1.174em 1000.003em 2.696em -0.524em); top: -2.105em; left: 0.003em;"><span class="mrow" id="MathJax-Span-37"><span class="mfrac" id="MathJax-Span-38"><span style="display: inline-block; position: relative; width: 0.53em; height: 0px; margin-right: 0.12em; margin-left: 0.12em;"><span style="position: absolute; clip: rect(1.759em 1000.003em 2.403em -0.524em); top: -2.632em; left: 50%; margin-left: -0.173em;"><span class="mi" id="MathJax-Span-39" style="font-size: 70.7%; font-family: MathJax_Math; font-style: italic;">n</span><span style="display: inline-block; width: 0px; height: 2.228em;"></span></span><span style="position: absolute; clip: rect(1.642em 1000.003em 2.462em -0.465em); top: -1.871em; left: 50%; margin-left: -0.173em;"><span class="mn" id="MathJax-Span-40" style="font-size: 70.7%; font-family: MathJax_Main;">8</span><span style="display: inline-block; width: 0px; height: 2.286em;"></span></span><span style="position: absolute; clip: rect(0.823em 1000.003em 1.232em -0.524em); top: -1.285em; left: 0.003em;"><span style="border-left-width: 0.53em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 1.25px; vertical-align: 0.003em;"></span><span style="display: inline-block; width: 0px; height: 1.057em;"></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 2.111em;"></span></span></span><span style="border-left-width: 0.004em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 1.575em; vertical-align: -0.568em;"></span></span></nobr></span><script type="math/tex" id="MathJax-Element-8">\frac {n}{8}</script></span></td>
</tr>
<tr class="row-odd">
<td>...</td>
<td> </td>
</tr>
<tr class="row-even">
<td>i</td>
<td><span class="math"><span class="MathJax_Preview"></span><span class="MathJax" id="MathJax-Element-9-Frame" role="textbox" aria-readonly="true"><nobr><span class="math" id="MathJax-Span-41" style="width: 1.232em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.998em; height: 0px; font-size: 122%;"><span style="position: absolute; clip: rect(1.174em 1000.003em 2.813em -0.524em); top: -2.105em; left: 0.003em;"><span class="mrow" id="MathJax-Span-42"><span class="mfrac" id="MathJax-Span-43"><span style="display: inline-block; position: relative; width: 0.764em; height: 0px; margin-right: 0.12em; margin-left: 0.12em;"><span style="position: absolute; clip: rect(1.759em 1000.003em 2.403em -0.524em); top: -2.632em; left: 50%; margin-left: -0.173em;"><span class="mi" id="MathJax-Span-44" style="font-size: 70.7%; font-family: MathJax_Math; font-style: italic;">n</span><span style="display: inline-block; width: 0px; height: 2.228em;"></span></span><span style="position: absolute; clip: rect(3.223em 1000.003em 4.16em -0.465em); top: -3.451em; left: 50%; margin-left: -0.29em;"><span class="msubsup" id="MathJax-Span-45"><span style="display: inline-block; position: relative; width: 0.647em; height: 0px;"><span style="position: absolute; clip: rect(1.642em 1000.003em 2.462em -0.465em); top: -2.28em; left: 0.003em;"><span class="mn" id="MathJax-Span-46" style="font-size: 70.7%; font-family: MathJax_Main;">2</span><span style="display: inline-block; width: 0px; height: 2.286em;"></span></span><span style="position: absolute; top: -2.398em; left: 0.413em;"><span class="mi" id="MathJax-Span-47" style="font-size: 50%; font-family: MathJax_Math; font-style: italic;">i</span><span style="display: inline-block; width: 0px; height: 2.111em;"></span></span></span></span><span style="display: inline-block; width: 0px; height: 3.984em;"></span></span><span style="position: absolute; clip: rect(0.823em 1000.003em 1.232em -0.524em); top: -1.285em; left: 0.003em;"><span style="border-left-width: 0.764em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 1.25px; vertical-align: 0.003em;"></span><span style="display: inline-block; width: 0px; height: 1.057em;"></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 2.111em;"></span></span></span><span style="border-left-width: 0.004em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 1.718em; vertical-align: -0.711em;"></span></span></nobr></span><script type="math/tex" id="MathJax-Element-9">\frac {n}{2^i}</script></span></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="Binary-Search-of-an-Ordered-List">Binary Search of an Ordered List<a class="anchor-link" href="python-data-structures.html#Binary-Search-of-an-Ordered-List">¶</a>
</h4>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [38]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">binarySearch</span><span class="p">(</span><span class="n">alist</span><span class="p">,</span> <span class="n">item</span><span class="p">):</span>
    <span class="n">first</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="n">last</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">alist</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span>
    <span class="n">found</span> <span class="o">=</span> <span class="kc">False</span>

    <span class="k">while</span> <span class="n">first</span> <span class="o">&lt;=</span> <span class="n">last</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">found</span><span class="p">:</span>
        <span class="n">midpoint</span> <span class="o">=</span> <span class="p">(</span><span class="n">first</span> <span class="o">+</span> <span class="n">last</span><span class="p">)</span> <span class="o">//</span> <span class="mi">2</span>
        <span class="k">if</span> <span class="n">alist</span><span class="p">[</span><span class="n">midpoint</span><span class="p">]</span> <span class="o">==</span> <span class="n">item</span><span class="p">:</span>
            <span class="n">found</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">item</span> <span class="o">&lt;</span> <span class="n">alist</span><span class="p">[</span><span class="n">midpoint</span><span class="p">]:</span>
                <span class="n">last</span> <span class="o">=</span> <span class="n">midpoint</span><span class="o">-</span><span class="mi">1</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">first</span> <span class="o">=</span> <span class="n">midpoint</span><span class="o">+</span><span class="mi">1</span>

    <span class="k">return</span> <span class="n">found</span>

<span class="n">testlist</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">13</span><span class="p">,</span> <span class="mi">17</span><span class="p">,</span> <span class="mi">19</span><span class="p">,</span> <span class="mi">32</span><span class="p">,</span> <span class="mi">42</span><span class="p">,]</span>
<span class="nb">print</span><span class="p">(</span><span class="n">binarySearch</span><span class="p">(</span><span class="n">testlist</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span>
<span class="nb">print</span><span class="p">(</span><span class="n">binarySearch</span><span class="p">(</span><span class="n">testlist</span><span class="p">,</span> <span class="mi">13</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>False
True
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="A-Binary-Search--Recursive-Version">A Binary Search--Recursive Version<a class="anchor-link" href="python-data-structures.html#A-Binary-Search--Recursive-Version">¶</a>
</h4>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [39]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">binarySearch</span><span class="p">(</span><span class="n">alist</span><span class="p">,</span> <span class="n">item</span><span class="p">):</span>
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">alist</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">return</span> <span class="kc">False</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">midpoint</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">alist</span><span class="p">)</span> <span class="o">//</span> <span class="mi">2</span>
        <span class="k">if</span> <span class="n">alist</span><span class="p">[</span><span class="n">midpoint</span><span class="p">]</span> <span class="o">==</span> <span class="n">item</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">item</span> <span class="o">&lt;</span> <span class="n">alist</span><span class="p">[</span><span class="n">midpoint</span><span class="p">]:</span>
                <span class="k">return</span> <span class="n">binarySearch</span><span class="p">(</span><span class="n">alist</span><span class="p">[:</span><span class="n">midpoint</span><span class="p">],</span> <span class="n">item</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">binarySearch</span><span class="p">(</span><span class="n">alist</span><span class="p">[</span><span class="n">midpoint</span> <span class="o">+</span> <span class="mi">1</span><span class="p">:],</span> <span class="n">item</span><span class="p">)</span>

<span class="n">testlist</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">13</span><span class="p">,</span> <span class="mi">17</span><span class="p">,</span> <span class="mi">19</span><span class="p">,</span> <span class="mi">32</span><span class="p">,</span> <span class="mi">42</span><span class="p">,</span> <span class="p">]</span>
<span class="nb">print</span><span class="p">(</span><span class="n">binarySearch</span><span class="p">(</span><span class="n">testlist</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span>
<span class="nb">print</span><span class="p">(</span><span class="n">binarySearch</span><span class="p">(</span><span class="n">testlist</span><span class="p">,</span> <span class="mi">13</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>False
True
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Hashing">Hashing<a class="anchor-link" href="python-data-structures.html#Hashing">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<ul>
<li>Map() Create a new, empty map. It returns an empty map collection.</li>
<li>put(key,val) Add a new key-value pair to the map. If the key is already in the map then replace the old value with the new value.</li>
<li>get(key) Given a key, return the value stored in the map or None otherwise.</li>
<li>del Delete the key-value pair from the map using a statement of the form del map[key].</li>
<li>len() Return the number of key-value pairs stored in the map.</li>
<li>in Return True for a statement of the form key in map, if the given key is in the map, False otherwise.</li>
</ul>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [40]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">class</span> <span class="nc">HashTable</span><span class="p">:</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">size</span> <span class="o">=</span> <span class="mi">11</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">slots</span> <span class="o">=</span> <span class="p">[</span><span class="kc">None</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">size</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">data</span> <span class="o">=</span> <span class="p">[</span><span class="kc">None</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">size</span>
        
    <span class="k">def</span> <span class="nf">put</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">key</span><span class="p">,</span><span class="n">data</span><span class="p">):</span>
      <span class="n">hashvalue</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">hashfunction</span><span class="p">(</span><span class="n">key</span><span class="p">,</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">slots</span><span class="p">))</span>

      <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">slots</span><span class="p">[</span><span class="n">hashvalue</span><span class="p">]</span> <span class="o">==</span> <span class="kc">None</span><span class="p">:</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">slots</span><span class="p">[</span><span class="n">hashvalue</span><span class="p">]</span> <span class="o">=</span> <span class="n">key</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">hashvalue</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span>
      <span class="k">else</span><span class="p">:</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">slots</span><span class="p">[</span><span class="n">hashvalue</span><span class="p">]</span> <span class="o">==</span> <span class="n">key</span><span class="p">:</span>
          <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">hashvalue</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span>  <span class="c1">#replace</span>
        <span class="k">else</span><span class="p">:</span>
          <span class="n">nextslot</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rehash</span><span class="p">(</span><span class="n">hashvalue</span><span class="p">,</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">slots</span><span class="p">))</span>
          <span class="k">while</span> <span class="bp">self</span><span class="o">.</span><span class="n">slots</span><span class="p">[</span><span class="n">nextslot</span><span class="p">]</span> <span class="o">!=</span> <span class="kc">None</span> <span class="ow">and</span> \
                          <span class="bp">self</span><span class="o">.</span><span class="n">slots</span><span class="p">[</span><span class="n">nextslot</span><span class="p">]</span> <span class="o">!=</span> <span class="n">key</span><span class="p">:</span>
            <span class="n">nextslot</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rehash</span><span class="p">(</span><span class="n">nextslot</span><span class="p">,</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">slots</span><span class="p">))</span>

          <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">slots</span><span class="p">[</span><span class="n">nextslot</span><span class="p">]</span> <span class="o">==</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">slots</span><span class="p">[</span><span class="n">nextslot</span><span class="p">]</span><span class="o">=</span><span class="n">key</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">nextslot</span><span class="p">]</span><span class="o">=</span><span class="n">data</span>
          <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">nextslot</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span> <span class="c1">#replace</span>

    <span class="k">def</span> <span class="nf">hashfunction</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">key</span><span class="p">,</span><span class="n">size</span><span class="p">):</span>
         <span class="k">return</span> <span class="n">key</span><span class="o">%</span><span class="k">size</span>

    <span class="k">def</span> <span class="nf">rehash</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">oldhash</span><span class="p">,</span><span class="n">size</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">(</span><span class="n">oldhash</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">%</span><span class="k">size</span>

    <span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">key</span><span class="p">):</span>
      <span class="n">startslot</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">hashfunction</span><span class="p">(</span><span class="n">key</span><span class="p">,</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">slots</span><span class="p">))</span>

      <span class="n">data</span> <span class="o">=</span> <span class="kc">None</span>
      <span class="n">stop</span> <span class="o">=</span> <span class="kc">False</span>
      <span class="n">found</span> <span class="o">=</span> <span class="kc">False</span>
      <span class="n">position</span> <span class="o">=</span> <span class="n">startslot</span>
      <span class="k">while</span> <span class="bp">self</span><span class="o">.</span><span class="n">slots</span><span class="p">[</span><span class="n">position</span><span class="p">]</span> <span class="o">!=</span> <span class="kc">None</span> <span class="ow">and</span>  \
                           <span class="ow">not</span> <span class="n">found</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">stop</span><span class="p">:</span>
         <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">slots</span><span class="p">[</span><span class="n">position</span><span class="p">]</span> <span class="o">==</span> <span class="n">key</span><span class="p">:</span>
           <span class="n">found</span> <span class="o">=</span> <span class="kc">True</span>
           <span class="n">data</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">position</span><span class="p">]</span>
         <span class="k">else</span><span class="p">:</span>
           <span class="n">position</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">rehash</span><span class="p">(</span><span class="n">position</span><span class="p">,</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">slots</span><span class="p">))</span>
           <span class="k">if</span> <span class="n">position</span> <span class="o">==</span> <span class="n">startslot</span><span class="p">:</span>
               <span class="n">stop</span> <span class="o">=</span> <span class="kc">True</span>
      <span class="k">return</span> <span class="n">data</span>

    <span class="k">def</span> <span class="nf">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">key</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__setitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">key</span><span class="p">,</span><span class="n">data</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="n">key</span><span class="p">,</span><span class="n">data</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [41]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">H</span><span class="o">=</span><span class="n">HashTable</span><span class="p">()</span>
<span class="n">H</span><span class="p">[</span><span class="mi">54</span><span class="p">]</span><span class="o">=</span><span class="s2">"cat"</span>
<span class="n">H</span><span class="p">[</span><span class="mi">26</span><span class="p">]</span><span class="o">=</span><span class="s2">"dog"</span>
<span class="n">H</span><span class="p">[</span><span class="mi">93</span><span class="p">]</span><span class="o">=</span><span class="s2">"lion"</span>
<span class="n">H</span><span class="p">[</span><span class="mi">17</span><span class="p">]</span><span class="o">=</span><span class="s2">"tiger"</span>
<span class="n">H</span><span class="p">[</span><span class="mi">77</span><span class="p">]</span><span class="o">=</span><span class="s2">"bird"</span>
<span class="n">H</span><span class="p">[</span><span class="mi">31</span><span class="p">]</span><span class="o">=</span><span class="s2">"cow"</span>
<span class="n">H</span><span class="p">[</span><span class="mi">44</span><span class="p">]</span><span class="o">=</span><span class="s2">"goat"</span>
<span class="n">H</span><span class="p">[</span><span class="mi">55</span><span class="p">]</span><span class="o">=</span><span class="s2">"pig"</span>
<span class="n">H</span><span class="p">[</span><span class="mi">20</span><span class="p">]</span><span class="o">=</span><span class="s2">"chicken"</span>
<span class="nb">print</span><span class="p">(</span><span class="n">H</span><span class="o">.</span><span class="n">slots</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">H</span><span class="o">.</span><span class="n">data</span><span class="p">)</span>

<span class="nb">print</span><span class="p">(</span><span class="n">H</span><span class="p">[</span><span class="mi">20</span><span class="p">])</span>

<span class="nb">print</span><span class="p">(</span><span class="n">H</span><span class="p">[</span><span class="mi">17</span><span class="p">])</span>
<span class="n">H</span><span class="p">[</span><span class="mi">20</span><span class="p">]</span><span class="o">=</span><span class="s1">'duck'</span>
<span class="nb">print</span><span class="p">(</span><span class="n">H</span><span class="p">[</span><span class="mi">20</span><span class="p">])</span>
<span class="nb">print</span><span class="p">(</span><span class="n">H</span><span class="p">[</span><span class="mi">99</span><span class="p">])</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>[77, 44, 55, 20, 26, 93, 17, None, None, 31, 54]
['bird', 'goat', 'pig', 'chicken', 'dog', 'lion', 'tiger', None, None, 'cow', 'cat']
chicken
tiger
duck
None
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="The-Bubble-Sort">The Bubble Sort<a class="anchor-link" href="python-data-structures.html#The-Bubble-Sort">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [42]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">bubbleSort</span><span class="p">(</span><span class="n">alist</span><span class="p">):</span>
    <span class="k">for</span> <span class="n">passnum</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">alist</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="o">-</span><span class="mi">1</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">passnum</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">alist</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">&gt;</span><span class="n">alist</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">]:</span>
                <span class="n">temp</span> <span class="o">=</span> <span class="n">alist</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
                <span class="n">alist</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">alist</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span>
                <span class="n">alist</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">temp</span>

<span class="n">alist</span> <span class="o">=</span> <span class="p">[</span><span class="mi">54</span><span class="p">,</span><span class="mi">26</span><span class="p">,</span><span class="mi">93</span><span class="p">,</span><span class="mi">17</span><span class="p">,</span><span class="mi">77</span><span class="p">,</span><span class="mi">31</span><span class="p">,</span><span class="mi">44</span><span class="p">,</span><span class="mi">55</span><span class="p">,</span><span class="mi">20</span><span class="p">]</span>
<span class="n">bubbleSort</span><span class="p">(</span><span class="n">alist</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">alist</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>[17, 20, 26, 31, 44, 54, 55, 77, 93]
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="The-Short-Bubble-Sort">The Short Bubble Sort<a class="anchor-link" href="python-data-structures.html#The-Short-Bubble-Sort">¶</a>
</h4>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>A bubble sort can be modified to stop early if it finds that the list has become sorted. This means that for lists that require just a few passes, a bubble sort may have an advantage in that it will recognize the sorted list and stop.</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [43]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">shortBubbleSort</span><span class="p">(</span><span class="n">alist</span><span class="p">):</span>
    <span class="n">exchanges</span> <span class="o">=</span> <span class="kc">True</span>
    <span class="n">passnum</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">alist</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span>
    <span class="k">while</span> <span class="n">passnum</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">exchanges</span><span class="p">:</span>
       <span class="n">exchanges</span> <span class="o">=</span> <span class="kc">False</span>
       <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">passnum</span><span class="p">):</span>
           <span class="k">if</span> <span class="n">alist</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">&gt;</span><span class="n">alist</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">]:</span>
               <span class="n">exchanges</span> <span class="o">=</span> <span class="kc">True</span>
               <span class="n">temp</span> <span class="o">=</span> <span class="n">alist</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
               <span class="n">alist</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">alist</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span>
               <span class="n">alist</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">temp</span>
       <span class="n">passnum</span> <span class="o">=</span> <span class="n">passnum</span><span class="o">-</span><span class="mi">1</span>

<span class="n">alist</span><span class="o">=</span><span class="p">[</span><span class="mi">20</span><span class="p">,</span><span class="mi">30</span><span class="p">,</span><span class="mi">40</span><span class="p">,</span><span class="mi">90</span><span class="p">,</span><span class="mi">50</span><span class="p">,</span><span class="mi">60</span><span class="p">,</span><span class="mi">70</span><span class="p">,</span><span class="mi">80</span><span class="p">,</span><span class="mi">100</span><span class="p">,</span><span class="mi">110</span><span class="p">]</span>
<span class="n">shortBubbleSort</span><span class="p">(</span><span class="n">alist</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">alist</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>[20, 30, 40, 50, 60, 70, 80, 90, 100, 110]
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="The-Selection-Sort">The Selection Sort<a class="anchor-link" href="python-data-structures.html#The-Selection-Sort">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Selection sort improves upon bubble sort by making fewer swaps</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [44]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">selectionSort</span><span class="p">(</span><span class="n">alist</span><span class="p">):</span>
   <span class="k">for</span> <span class="n">fillslot</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">alist</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="o">-</span><span class="mi">1</span><span class="p">):</span>
       <span class="n">positionOfMax</span><span class="o">=</span><span class="mi">0</span>
       <span class="k">for</span> <span class="n">location</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="n">fillslot</span><span class="o">+</span><span class="mi">1</span><span class="p">):</span>
           <span class="k">if</span> <span class="n">alist</span><span class="p">[</span><span class="n">location</span><span class="p">]</span><span class="o">&gt;</span><span class="n">alist</span><span class="p">[</span><span class="n">positionOfMax</span><span class="p">]:</span>
               <span class="n">positionOfMax</span> <span class="o">=</span> <span class="n">location</span>

       <span class="n">temp</span> <span class="o">=</span> <span class="n">alist</span><span class="p">[</span><span class="n">fillslot</span><span class="p">]</span>
       <span class="n">alist</span><span class="p">[</span><span class="n">fillslot</span><span class="p">]</span> <span class="o">=</span> <span class="n">alist</span><span class="p">[</span><span class="n">positionOfMax</span><span class="p">]</span>
       <span class="n">alist</span><span class="p">[</span><span class="n">positionOfMax</span><span class="p">]</span> <span class="o">=</span> <span class="n">temp</span>

<span class="n">alist</span> <span class="o">=</span> <span class="p">[</span><span class="mi">54</span><span class="p">,</span><span class="mi">26</span><span class="p">,</span><span class="mi">93</span><span class="p">,</span><span class="mi">17</span><span class="p">,</span><span class="mi">77</span><span class="p">,</span><span class="mi">31</span><span class="p">,</span><span class="mi">44</span><span class="p">,</span><span class="mi">55</span><span class="p">,</span><span class="mi">20</span><span class="p">]</span>
<span class="n">selectionSort</span><span class="p">(</span><span class="n">alist</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">alist</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>[17, 20, 26, 31, 44, 54, 55, 77, 93]
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="The-Insertion-Sort">The Insertion Sort<a class="anchor-link" href="python-data-structures.html#The-Insertion-Sort">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Insertion sort works at the start of the list. Each pass produces a longer sorted list</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [45]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">insertionSort</span><span class="p">(</span><span class="n">alist</span><span class="p">):</span>
   <span class="k">for</span> <span class="n">index</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="nb">len</span><span class="p">(</span><span class="n">alist</span><span class="p">)):</span>

     <span class="n">currentvalue</span> <span class="o">=</span> <span class="n">alist</span><span class="p">[</span><span class="n">index</span><span class="p">]</span>
     <span class="n">position</span> <span class="o">=</span> <span class="n">index</span>

     <span class="k">while</span> <span class="n">position</span><span class="o">&gt;</span><span class="mi">0</span> <span class="ow">and</span> <span class="n">alist</span><span class="p">[</span><span class="n">position</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">&gt;</span><span class="n">currentvalue</span><span class="p">:</span>
         <span class="n">alist</span><span class="p">[</span><span class="n">position</span><span class="p">]</span><span class="o">=</span><span class="n">alist</span><span class="p">[</span><span class="n">position</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
         <span class="n">position</span> <span class="o">=</span> <span class="n">position</span><span class="o">-</span><span class="mi">1</span>

     <span class="n">alist</span><span class="p">[</span><span class="n">position</span><span class="p">]</span><span class="o">=</span><span class="n">currentvalue</span>

<span class="n">alist</span> <span class="o">=</span> <span class="p">[</span><span class="mi">54</span><span class="p">,</span><span class="mi">26</span><span class="p">,</span><span class="mi">93</span><span class="p">,</span><span class="mi">17</span><span class="p">,</span><span class="mi">77</span><span class="p">,</span><span class="mi">31</span><span class="p">,</span><span class="mi">44</span><span class="p">,</span><span class="mi">55</span><span class="p">,</span><span class="mi">20</span><span class="p">]</span>
<span class="n">insertionSort</span><span class="p">(</span><span class="n">alist</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">alist</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>[17, 20, 26, 31, 44, 54, 55, 77, 93]
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="The-Shell-Sort">The Shell Sort<a class="anchor-link" href="python-data-structures.html#The-Shell-Sort">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [46]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">shellSort</span><span class="p">(</span><span class="n">alist</span><span class="p">):</span>
    <span class="n">sublistcount</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">alist</span><span class="p">)</span> <span class="o">//</span> <span class="mi">2</span>
    <span class="k">while</span> <span class="n">sublistcount</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>

      <span class="k">for</span> <span class="n">startposition</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">sublistcount</span><span class="p">):</span>
        <span class="n">gapInsertionSort</span><span class="p">(</span><span class="n">alist</span><span class="p">,</span><span class="n">startposition</span><span class="p">,</span><span class="n">sublistcount</span><span class="p">)</span>

      <span class="nb">print</span><span class="p">(</span><span class="s2">"After increments of size"</span><span class="p">,</span><span class="n">sublistcount</span><span class="p">,</span> <span class="s2">"The list is"</span><span class="p">,</span> <span class="n">alist</span><span class="p">)</span>

      <span class="n">sublistcount</span> <span class="o">=</span> <span class="n">sublistcount</span> <span class="o">//</span> <span class="mi">2</span>

<span class="k">def</span> <span class="nf">gapInsertionSort</span><span class="p">(</span><span class="n">alist</span><span class="p">,</span><span class="n">start</span><span class="p">,</span><span class="n">gap</span><span class="p">):</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">start</span><span class="o">+</span><span class="n">gap</span><span class="p">,</span><span class="nb">len</span><span class="p">(</span><span class="n">alist</span><span class="p">),</span><span class="n">gap</span><span class="p">):</span>

        <span class="n">currentvalue</span> <span class="o">=</span> <span class="n">alist</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
        <span class="n">position</span> <span class="o">=</span> <span class="n">i</span>

        <span class="k">while</span> <span class="n">position</span><span class="o">&gt;=</span><span class="n">gap</span> <span class="ow">and</span> <span class="n">alist</span><span class="p">[</span><span class="n">position</span><span class="o">-</span><span class="n">gap</span><span class="p">]</span><span class="o">&gt;</span><span class="n">currentvalue</span><span class="p">:</span>
            <span class="n">alist</span><span class="p">[</span><span class="n">position</span><span class="p">]</span><span class="o">=</span><span class="n">alist</span><span class="p">[</span><span class="n">position</span><span class="o">-</span><span class="n">gap</span><span class="p">]</span>
            <span class="n">position</span> <span class="o">=</span> <span class="n">position</span><span class="o">-</span><span class="n">gap</span>

        <span class="n">alist</span><span class="p">[</span><span class="n">position</span><span class="p">]</span><span class="o">=</span><span class="n">currentvalue</span>

<span class="n">alist</span> <span class="o">=</span> <span class="p">[</span><span class="mi">54</span><span class="p">,</span><span class="mi">26</span><span class="p">,</span><span class="mi">93</span><span class="p">,</span><span class="mi">17</span><span class="p">,</span><span class="mi">77</span><span class="p">,</span><span class="mi">31</span><span class="p">,</span><span class="mi">44</span><span class="p">,</span><span class="mi">55</span><span class="p">,</span><span class="mi">20</span><span class="p">]</span>
<span class="n">shellSort</span><span class="p">(</span><span class="n">alist</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">alist</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>After increments of size 4 The list is [20, 26, 44, 17, 54, 31, 93, 55, 77]
After increments of size 2 The list is [20, 17, 44, 26, 54, 31, 77, 55, 93]
After increments of size 1 The list is [17, 20, 26, 31, 44, 54, 55, 77, 93]
[17, 20, 26, 31, 44, 54, 55, 77, 93]
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="The-Merge-Sort">The Merge Sort<a class="anchor-link" href="python-data-structures.html#The-Merge-Sort">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Merge sort will continue to recursively move toward the beginning of the list until it hits a base case.</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [47]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">mergeSort</span><span class="p">(</span><span class="n">alist</span><span class="p">):</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">"Splitting "</span><span class="p">,</span><span class="n">alist</span><span class="p">)</span>
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">alist</span><span class="p">)</span><span class="o">&gt;</span><span class="mi">1</span><span class="p">:</span>
        <span class="n">mid</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">alist</span><span class="p">)</span><span class="o">//</span><span class="mi">2</span>
        <span class="n">lefthalf</span> <span class="o">=</span> <span class="n">alist</span><span class="p">[:</span><span class="n">mid</span><span class="p">]</span>
        <span class="n">righthalf</span> <span class="o">=</span> <span class="n">alist</span><span class="p">[</span><span class="n">mid</span><span class="p">:]</span>

        <span class="n">mergeSort</span><span class="p">(</span><span class="n">lefthalf</span><span class="p">)</span>
        <span class="n">mergeSort</span><span class="p">(</span><span class="n">righthalf</span><span class="p">)</span>

        <span class="n">i</span><span class="o">=</span><span class="mi">0</span>
        <span class="n">j</span><span class="o">=</span><span class="mi">0</span>
        <span class="n">k</span><span class="o">=</span><span class="mi">0</span>
        <span class="k">while</span> <span class="n">i</span><span class="o">&lt;</span><span class="nb">len</span><span class="p">(</span><span class="n">lefthalf</span><span class="p">)</span> <span class="ow">and</span> <span class="n">j</span><span class="o">&lt;</span><span class="nb">len</span><span class="p">(</span><span class="n">righthalf</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">lefthalf</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">&lt;</span><span class="n">righthalf</span><span class="p">[</span><span class="n">j</span><span class="p">]:</span>
                <span class="n">alist</span><span class="p">[</span><span class="n">k</span><span class="p">]</span><span class="o">=</span><span class="n">lefthalf</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
                <span class="n">i</span><span class="o">=</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">alist</span><span class="p">[</span><span class="n">k</span><span class="p">]</span><span class="o">=</span><span class="n">righthalf</span><span class="p">[</span><span class="n">j</span><span class="p">]</span>
                <span class="n">j</span><span class="o">=</span><span class="n">j</span><span class="o">+</span><span class="mi">1</span>
            <span class="n">k</span><span class="o">=</span><span class="n">k</span><span class="o">+</span><span class="mi">1</span>

        <span class="k">while</span> <span class="n">i</span><span class="o">&lt;</span><span class="nb">len</span><span class="p">(</span><span class="n">lefthalf</span><span class="p">):</span>
            <span class="n">alist</span><span class="p">[</span><span class="n">k</span><span class="p">]</span><span class="o">=</span><span class="n">lefthalf</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
            <span class="n">i</span><span class="o">=</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span>
            <span class="n">k</span><span class="o">=</span><span class="n">k</span><span class="o">+</span><span class="mi">1</span>

        <span class="k">while</span> <span class="n">j</span><span class="o">&lt;</span><span class="nb">len</span><span class="p">(</span><span class="n">righthalf</span><span class="p">):</span>
            <span class="n">alist</span><span class="p">[</span><span class="n">k</span><span class="p">]</span><span class="o">=</span><span class="n">righthalf</span><span class="p">[</span><span class="n">j</span><span class="p">]</span>
            <span class="n">j</span><span class="o">=</span><span class="n">j</span><span class="o">+</span><span class="mi">1</span>
            <span class="n">k</span><span class="o">=</span><span class="n">k</span><span class="o">+</span><span class="mi">1</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">"Merging "</span><span class="p">,</span><span class="n">alist</span><span class="p">)</span>

<span class="n">alist</span> <span class="o">=</span> <span class="p">[</span><span class="mi">54</span><span class="p">,</span><span class="mi">26</span><span class="p">,</span><span class="mi">93</span><span class="p">,</span><span class="mi">17</span><span class="p">,</span><span class="mi">77</span><span class="p">,</span><span class="mi">31</span><span class="p">,</span><span class="mi">44</span><span class="p">,</span><span class="mi">55</span><span class="p">,</span><span class="mi">20</span><span class="p">]</span>
<span class="n">mergeSort</span><span class="p">(</span><span class="n">alist</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">alist</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>Splitting  [54, 26, 93, 17, 77, 31, 44, 55, 20]
Splitting  [54, 26, 93, 17]
Splitting  [54, 26]
Splitting  [54]
Merging  [54]
Splitting  [26]
Merging  [26]
Merging  [26, 54]
Splitting  [93, 17]
Splitting  [93]
Merging  [93]
Splitting  [17]
Merging  [17]
Merging  [17, 93]
Merging  [17, 26, 54, 93]
Splitting  [77, 31, 44, 55, 20]
Splitting  [77, 31]
Splitting  [77]
Merging  [77]
Splitting  [31]
Merging  [31]
Merging  [31, 77]
Splitting  [44, 55, 20]
Splitting  [44]
Merging  [44]
Splitting  [55, 20]
Splitting  [55]
Merging  [55]
Splitting  [20]
Merging  [20]
Merging  [20, 55]
Merging  [20, 44, 55]
Merging  [20, 31, 44, 55, 77]
Merging  [17, 20, 26, 31, 44, 54, 55, 77, 93]
[17, 20, 26, 31, 44, 54, 55, 77, 93]
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="The-Quick-Sort">The Quick Sort<a class="anchor-link" href="python-data-structures.html#The-Quick-Sort">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>To choose the pivot value, we will consider the first, the middle, and the last element in the list. In our example, those are 54, 77, and 20. Now pick the median value, in our case 54, and use it for the pivot value (of course, that was the pivot value we used originally). The idea is that in the case where the the first item in the list does not belong toward the middle of the list, the median of three will choose a better “middle” value. This will be particularly useful when the original list is somewhat sorted to begin with.</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [48]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">quickSort</span><span class="p">(</span><span class="n">alist</span><span class="p">):</span>
   <span class="n">quickSortHelper</span><span class="p">(</span><span class="n">alist</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="nb">len</span><span class="p">(</span><span class="n">alist</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">quickSortHelper</span><span class="p">(</span><span class="n">alist</span><span class="p">,</span><span class="n">first</span><span class="p">,</span><span class="n">last</span><span class="p">):</span>
   <span class="k">if</span> <span class="n">first</span><span class="o">&lt;</span><span class="n">last</span><span class="p">:</span>

       <span class="n">splitpoint</span> <span class="o">=</span> <span class="n">partition</span><span class="p">(</span><span class="n">alist</span><span class="p">,</span><span class="n">first</span><span class="p">,</span><span class="n">last</span><span class="p">)</span>

       <span class="n">quickSortHelper</span><span class="p">(</span><span class="n">alist</span><span class="p">,</span><span class="n">first</span><span class="p">,</span><span class="n">splitpoint</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
       <span class="n">quickSortHelper</span><span class="p">(</span><span class="n">alist</span><span class="p">,</span><span class="n">splitpoint</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span><span class="n">last</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">partition</span><span class="p">(</span><span class="n">alist</span><span class="p">,</span><span class="n">first</span><span class="p">,</span><span class="n">last</span><span class="p">):</span>
   <span class="n">pivotvalue</span> <span class="o">=</span> <span class="n">alist</span><span class="p">[</span><span class="n">first</span><span class="p">]</span>

   <span class="n">leftmark</span> <span class="o">=</span> <span class="n">first</span><span class="o">+</span><span class="mi">1</span>
   <span class="n">rightmark</span> <span class="o">=</span> <span class="n">last</span>

   <span class="n">done</span> <span class="o">=</span> <span class="kc">False</span>
   <span class="k">while</span> <span class="ow">not</span> <span class="n">done</span><span class="p">:</span>

       <span class="k">while</span> <span class="n">leftmark</span> <span class="o">&lt;=</span> <span class="n">rightmark</span> <span class="ow">and</span> \
               <span class="n">alist</span><span class="p">[</span><span class="n">leftmark</span><span class="p">]</span> <span class="o">&lt;=</span> <span class="n">pivotvalue</span><span class="p">:</span>
           <span class="n">leftmark</span> <span class="o">=</span> <span class="n">leftmark</span> <span class="o">+</span> <span class="mi">1</span>

       <span class="k">while</span> <span class="n">alist</span><span class="p">[</span><span class="n">rightmark</span><span class="p">]</span> <span class="o">&gt;=</span> <span class="n">pivotvalue</span> <span class="ow">and</span> \
               <span class="n">rightmark</span> <span class="o">&gt;=</span> <span class="n">leftmark</span><span class="p">:</span>
           <span class="n">rightmark</span> <span class="o">=</span> <span class="n">rightmark</span> <span class="o">-</span><span class="mi">1</span>

       <span class="k">if</span> <span class="n">rightmark</span> <span class="o">&lt;</span> <span class="n">leftmark</span><span class="p">:</span>
           <span class="n">done</span> <span class="o">=</span> <span class="kc">True</span>
       <span class="k">else</span><span class="p">:</span>
           <span class="n">temp</span> <span class="o">=</span> <span class="n">alist</span><span class="p">[</span><span class="n">leftmark</span><span class="p">]</span>
           <span class="n">alist</span><span class="p">[</span><span class="n">leftmark</span><span class="p">]</span> <span class="o">=</span> <span class="n">alist</span><span class="p">[</span><span class="n">rightmark</span><span class="p">]</span>
           <span class="n">alist</span><span class="p">[</span><span class="n">rightmark</span><span class="p">]</span> <span class="o">=</span> <span class="n">temp</span>

   <span class="n">temp</span> <span class="o">=</span> <span class="n">alist</span><span class="p">[</span><span class="n">first</span><span class="p">]</span>
   <span class="n">alist</span><span class="p">[</span><span class="n">first</span><span class="p">]</span> <span class="o">=</span> <span class="n">alist</span><span class="p">[</span><span class="n">rightmark</span><span class="p">]</span>
   <span class="n">alist</span><span class="p">[</span><span class="n">rightmark</span><span class="p">]</span> <span class="o">=</span> <span class="n">temp</span>


   <span class="k">return</span> <span class="n">rightmark</span>

<span class="n">alist</span> <span class="o">=</span> <span class="p">[</span><span class="mi">54</span><span class="p">,</span><span class="mi">26</span><span class="p">,</span><span class="mi">93</span><span class="p">,</span><span class="mi">17</span><span class="p">,</span><span class="mi">77</span><span class="p">,</span><span class="mi">31</span><span class="p">,</span><span class="mi">44</span><span class="p">,</span><span class="mi">55</span><span class="p">,</span><span class="mi">20</span><span class="p">]</span>
<span class="n">quickSort</span><span class="p">(</span><span class="n">alist</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">alist</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>[17, 20, 26, 31, 44, 54, 55, 77, 93]
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<ol>
<li>A sequential search is $O(n)$ for ordered and unordered lists.</li>
<li>A binary search of an ordered list is $O(logn)$ in the worst case.</li>
<li>Hash tables can provide constant time searching.</li>
<li>A bubble sort, a selection sort, and an insertion sort are $O(n^2)$ algorithms.</li>
<li>A shell sort improves on the insertion sort by sorting incremental sublists. It falls between $O(n)$ and $O(n^2)$.</li>
<li>A merge sort is $O(nlogn)$, but requires additional space for the merging process.</li>
<li>A quick sort is $O(nlogn)$, but may degrade to $O(n^2)$ if the split points are not near the middle of the list. It does not require additional space.</li>
</ol>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Trees-and-Tree-Algorithms">Trees and Tree Algorithms<a class="anchor-link" href="python-data-structures.html#Trees-and-Tree-Algorithms">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<ol>
<li>Definition One: A tree consists of a set of nodes and a set of edges that connect pairs of nodes. A tree has the following properties:<ul>
<li>One node of the tree is designated as the root node.</li>
<li>Every node n, except the root node, is connected by an edge from exactly one other node p, where p is the parent of n.</li>
<li>A unique path traverses from the root to each node.</li>
<li>If each node in the tree has a maximum of two children, we say that the tree is a binary tree.</li>
</ul>
</li>
<li>Definition Two: A tree is either empty or consists of a root and zero or more subtrees, each of which is also a tree. The root of each subtree is connected to the root of the parent tree by an edge. </li>
</ol>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="List-of-Lists-Representation">List of Lists Representation<a class="anchor-link" href="python-data-structures.html#List-of-Lists-Representation">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [49]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">myTree</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'a'</span><span class="p">,</span> <span class="p">[</span><span class="s1">'b'</span><span class="p">,</span> <span class="p">[</span><span class="s1">'d'</span><span class="p">,[],[]],</span> <span class="p">[</span><span class="s1">'e'</span><span class="p">,[],[]]</span> <span class="p">],</span> <span class="p">[</span><span class="s1">'c'</span><span class="p">,</span> <span class="p">[</span><span class="s1">'f'</span><span class="p">,[],[]],</span> <span class="p">[]]</span> <span class="p">]</span>
<span class="nb">print</span><span class="p">(</span><span class="n">myTree</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">'left subtree = '</span><span class="p">,</span> <span class="n">myTree</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">'root = '</span><span class="p">,</span> <span class="n">myTree</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">'right subtree = '</span><span class="p">,</span> <span class="n">myTree</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>['a', ['b', ['d', [], []], ['e', [], []]], ['c', ['f', [], []], []]]
left subtree =  ['b', ['d', [], []], ['e', [], []]]
root =  a
right subtree =  ['c', ['f', [], []], []]
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="A-Python-Session-to-Illustrate-Basic-Tree-Functions">A Python Session to Illustrate Basic Tree Functions<a class="anchor-link" href="python-data-structures.html#A-Python-Session-to-Illustrate-Basic-Tree-Functions">¶</a>
</h4>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [50]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">BinaryTree</span><span class="p">(</span><span class="n">r</span><span class="p">):</span>
    <span class="k">return</span> <span class="p">[</span><span class="n">r</span><span class="p">,</span> <span class="p">[],</span> <span class="p">[]]</span>

<span class="k">def</span> <span class="nf">insertLeft</span><span class="p">(</span><span class="n">root</span><span class="p">,</span><span class="n">newBranch</span><span class="p">):</span>
    <span class="n">t</span> <span class="o">=</span> <span class="n">root</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">t</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
        <span class="n">root</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">1</span><span class="p">,[</span><span class="n">newBranch</span><span class="p">,</span><span class="n">t</span><span class="p">,[]])</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">root</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">1</span><span class="p">,[</span><span class="n">newBranch</span><span class="p">,</span> <span class="p">[],</span> <span class="p">[]])</span>
    <span class="k">return</span> <span class="n">root</span>

<span class="k">def</span> <span class="nf">insertRight</span><span class="p">(</span><span class="n">root</span><span class="p">,</span><span class="n">newBranch</span><span class="p">):</span>
    <span class="n">t</span> <span class="o">=</span> <span class="n">root</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">t</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
        <span class="n">root</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">2</span><span class="p">,[</span><span class="n">newBranch</span><span class="p">,[],</span><span class="n">t</span><span class="p">])</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">root</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">2</span><span class="p">,[</span><span class="n">newBranch</span><span class="p">,[],[]])</span>
    <span class="k">return</span> <span class="n">root</span>

<span class="k">def</span> <span class="nf">getRootVal</span><span class="p">(</span><span class="n">root</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">root</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

<span class="k">def</span> <span class="nf">setRootVal</span><span class="p">(</span><span class="n">root</span><span class="p">,</span><span class="n">newVal</span><span class="p">):</span>
    <span class="n">root</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">newVal</span>

<span class="k">def</span> <span class="nf">getLeftChild</span><span class="p">(</span><span class="n">root</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">root</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>

<span class="k">def</span> <span class="nf">getRightChild</span><span class="p">(</span><span class="n">root</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">root</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>

<span class="n">r</span> <span class="o">=</span> <span class="n">BinaryTree</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="n">insertLeft</span><span class="p">(</span><span class="n">r</span><span class="p">,</span><span class="mi">4</span><span class="p">)</span>
<span class="n">insertLeft</span><span class="p">(</span><span class="n">r</span><span class="p">,</span><span class="mi">5</span><span class="p">)</span>
<span class="n">insertRight</span><span class="p">(</span><span class="n">r</span><span class="p">,</span><span class="mi">6</span><span class="p">)</span>
<span class="n">insertRight</span><span class="p">(</span><span class="n">r</span><span class="p">,</span><span class="mi">7</span><span class="p">)</span>
<span class="n">l</span> <span class="o">=</span> <span class="n">getLeftChild</span><span class="p">(</span><span class="n">r</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">l</span><span class="p">)</span>

<span class="n">setRootVal</span><span class="p">(</span><span class="n">l</span><span class="p">,</span><span class="mi">9</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">r</span><span class="p">)</span>
<span class="n">insertLeft</span><span class="p">(</span><span class="n">l</span><span class="p">,</span><span class="mi">11</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">r</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">getRightChild</span><span class="p">(</span><span class="n">getRightChild</span><span class="p">(</span><span class="n">r</span><span class="p">)))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>[5, [4, [], []], []]
[3, [9, [4, [], []], []], [7, [], [6, [], []]]]
[3, [9, [11, [4, [], []], []], []], [7, [], [6, [], []]]]
[6, [], []]
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [51]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">x</span> <span class="o">=</span> <span class="n">BinaryTree</span><span class="p">(</span><span class="s1">'a'</span><span class="p">)</span>
<span class="n">insertLeft</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="s1">'b'</span><span class="p">)</span>
<span class="n">insertRight</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="s1">'c'</span><span class="p">)</span>
<span class="n">insertRight</span><span class="p">(</span><span class="n">getRightChild</span><span class="p">(</span><span class="n">x</span><span class="p">),</span><span class="s1">'d'</span><span class="p">)</span>
<span class="n">insertLeft</span><span class="p">(</span><span class="n">getRightChild</span><span class="p">(</span><span class="n">getRightChild</span><span class="p">(</span><span class="n">x</span><span class="p">)),</span><span class="s1">'e'</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>['a', ['b', [], []], ['c', [], ['d', ['e', [], []], []]]]
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Nodes-and-References">Nodes and References<a class="anchor-link" href="python-data-structures.html#Nodes-and-References">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [52]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">class</span> <span class="nc">BinaryTree</span><span class="p">:</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">rootObj</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">key</span> <span class="o">=</span> <span class="n">rootObj</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">leftChild</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">rightChild</span> <span class="o">=</span> <span class="kc">None</span>

    <span class="k">def</span> <span class="nf">insertLeft</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">newNode</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">leftChild</span> <span class="o">==</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">leftChild</span> <span class="o">=</span> <span class="n">BinaryTree</span><span class="p">(</span><span class="n">newNode</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">t</span> <span class="o">=</span> <span class="n">BinaryTree</span><span class="p">(</span><span class="n">newNode</span><span class="p">)</span>
            <span class="n">t</span><span class="o">.</span><span class="n">leftChild</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">leftChild</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">leftChild</span> <span class="o">=</span> <span class="n">t</span>

    <span class="k">def</span> <span class="nf">insertRight</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">newNode</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">rightChild</span> <span class="o">==</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">rightChild</span> <span class="o">=</span> <span class="n">BinaryTree</span><span class="p">(</span><span class="n">newNode</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">t</span> <span class="o">=</span> <span class="n">BinaryTree</span><span class="p">(</span><span class="n">newNode</span><span class="p">)</span>
            <span class="n">t</span><span class="o">.</span><span class="n">rightChild</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rightChild</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">rightChild</span> <span class="o">=</span> <span class="n">t</span>


    <span class="k">def</span> <span class="nf">getRightChild</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">rightChild</span>

    <span class="k">def</span> <span class="nf">getLeftChild</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">leftChild</span>

    <span class="k">def</span> <span class="nf">setRootVal</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">obj</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">key</span> <span class="o">=</span> <span class="n">obj</span>

    <span class="k">def</span> <span class="nf">getRootVal</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">key</span>
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [53]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">r</span> <span class="o">=</span> <span class="n">BinaryTree</span><span class="p">(</span><span class="s1">'a'</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">r</span><span class="o">.</span><span class="n">getRootVal</span><span class="p">())</span>
<span class="nb">print</span><span class="p">(</span><span class="n">r</span><span class="o">.</span><span class="n">getLeftChild</span><span class="p">())</span>
<span class="n">r</span><span class="o">.</span><span class="n">insertLeft</span><span class="p">(</span><span class="s1">'b'</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">r</span><span class="o">.</span><span class="n">getLeftChild</span><span class="p">())</span>
<span class="nb">print</span><span class="p">(</span><span class="n">r</span><span class="o">.</span><span class="n">getLeftChild</span><span class="p">()</span><span class="o">.</span><span class="n">getRootVal</span><span class="p">())</span>
<span class="n">r</span><span class="o">.</span><span class="n">insertRight</span><span class="p">(</span><span class="s1">'c'</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">r</span><span class="o">.</span><span class="n">getRightChild</span><span class="p">())</span>
<span class="nb">print</span><span class="p">(</span><span class="n">r</span><span class="o">.</span><span class="n">getRightChild</span><span class="p">()</span><span class="o">.</span><span class="n">getRootVal</span><span class="p">())</span>
<span class="n">r</span><span class="o">.</span><span class="n">getRightChild</span><span class="p">()</span><span class="o">.</span><span class="n">setRootVal</span><span class="p">(</span><span class="s1">'hello'</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">r</span><span class="o">.</span><span class="n">getRightChild</span><span class="p">()</span><span class="o">.</span><span class="n">getRootVal</span><span class="p">())</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>a
None
&lt;__main__.BinaryTree object at 0x7f83a44e9c88&gt;
b
&lt;__main__.BinaryTree object at 0x7f83a44e5898&gt;
c
hello
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Parse-Tree">Parse Tree<a class="anchor-link" href="python-data-structures.html#Parse-Tree">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<ol>
<li>If the current token is a <code>'('</code>, add a new node as the left child of the current node, and descend to the left child.</li>
<li>If the current token is in the list <code>['+','-','/','*']</code>, set the root value of the current node to the operator represented by the current token. Add a new node as the right child of the current node and descend to the right child.</li>
<li>If the current token is a number, set the root value of the current node to the number and return to the parent.</li>
<li>If the current token is a <code>')'</code>, go to the parent of the current node.</li>
</ol>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [54]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">pythonds.basic.stack</span> <span class="k">import</span> <span class="n">Stack</span>
<span class="kn">from</span> <span class="nn">pythonds.trees.binaryTree</span> <span class="k">import</span> <span class="n">BinaryTree</span>
<span class="k">def</span> <span class="nf">buildParseTree</span><span class="p">(</span><span class="n">fpexp</span><span class="p">):</span>
    <span class="n">fplist</span> <span class="o">=</span> <span class="n">fpexp</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
    <span class="n">pStack</span> <span class="o">=</span> <span class="n">Stack</span><span class="p">()</span>
    <span class="n">eTree</span> <span class="o">=</span> <span class="n">BinaryTree</span><span class="p">(</span><span class="s1">''</span><span class="p">)</span>
    <span class="n">pStack</span><span class="o">.</span><span class="n">push</span><span class="p">(</span><span class="n">eTree</span><span class="p">)</span>
    <span class="n">currentTree</span> <span class="o">=</span> <span class="n">eTree</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">fplist</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="s1">'('</span><span class="p">:</span>
            <span class="n">currentTree</span><span class="o">.</span><span class="n">insertLeft</span><span class="p">(</span><span class="s1">''</span><span class="p">)</span>
            <span class="n">pStack</span><span class="o">.</span><span class="n">push</span><span class="p">(</span><span class="n">currentTree</span><span class="p">)</span>
            <span class="n">currentTree</span> <span class="o">=</span> <span class="n">currentTree</span><span class="o">.</span><span class="n">getLeftChild</span><span class="p">()</span>
        <span class="k">elif</span> <span class="n">i</span> <span class="ow">not</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">'+'</span><span class="p">,</span> <span class="s1">'-'</span><span class="p">,</span> <span class="s1">'*'</span><span class="p">,</span> <span class="s1">'/'</span><span class="p">,</span> <span class="s1">')'</span><span class="p">]:</span>
            <span class="n">currentTree</span><span class="o">.</span><span class="n">setRootVal</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">i</span><span class="p">))</span>
            <span class="n">parent</span> <span class="o">=</span> <span class="n">pStack</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
            <span class="n">currentTree</span> <span class="o">=</span> <span class="n">parent</span>
        <span class="k">elif</span> <span class="n">i</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">'+'</span><span class="p">,</span> <span class="s1">'-'</span><span class="p">,</span> <span class="s1">'*'</span><span class="p">,</span> <span class="s1">'/'</span><span class="p">]:</span>
            <span class="n">currentTree</span><span class="o">.</span><span class="n">setRootVal</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
            <span class="n">currentTree</span><span class="o">.</span><span class="n">insertRight</span><span class="p">(</span><span class="s1">''</span><span class="p">)</span>
            <span class="n">pStack</span><span class="o">.</span><span class="n">push</span><span class="p">(</span><span class="n">currentTree</span><span class="p">)</span>
            <span class="n">currentTree</span> <span class="o">=</span> <span class="n">currentTree</span><span class="o">.</span><span class="n">getRightChild</span><span class="p">()</span>
        <span class="k">elif</span> <span class="n">i</span> <span class="o">==</span> <span class="s1">')'</span><span class="p">:</span>
            <span class="n">currentTree</span> <span class="o">=</span> <span class="n">pStack</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span>
    <span class="k">return</span> <span class="n">eTree</span>
<span class="n">pt</span> <span class="o">=</span> <span class="n">buildParseTree</span><span class="p">(</span><span class="s2">"( ( 10 + 5 ) * 3 )"</span><span class="p">)</span>
<span class="n">pt</span><span class="o">.</span><span class="n">postorder</span><span class="p">()</span>  <span class="c1">#defined and explained in the next section</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>3
7
9
(((4)+(5))*(7))
63
2
10
5
+
3
*
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [55]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">operator</span>
<span class="k">def</span> <span class="nf">evaluate</span><span class="p">(</span><span class="n">parseTree</span><span class="p">):</span>
    <span class="n">opers</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'+'</span><span class="p">:</span><span class="n">operator</span><span class="o">.</span><span class="n">add</span><span class="p">,</span> <span class="s1">'-'</span><span class="p">:</span><span class="n">operator</span><span class="o">.</span><span class="n">sub</span><span class="p">,</span> <span class="s1">'*'</span><span class="p">:</span><span class="n">operator</span><span class="o">.</span><span class="n">mul</span><span class="p">,</span> <span class="s1">'/'</span><span class="p">:</span><span class="n">operator</span><span class="o">.</span><span class="n">truediv</span><span class="p">}</span>

    <span class="n">leftC</span> <span class="o">=</span> <span class="n">parseTree</span><span class="o">.</span><span class="n">getLeftChild</span><span class="p">()</span>
    <span class="n">rightC</span> <span class="o">=</span> <span class="n">parseTree</span><span class="o">.</span><span class="n">getRightChild</span><span class="p">()</span>

    <span class="k">if</span> <span class="n">leftC</span> <span class="ow">and</span> <span class="n">rightC</span><span class="p">:</span>
        <span class="n">fn</span> <span class="o">=</span> <span class="n">opers</span><span class="p">[</span><span class="n">parseTree</span><span class="o">.</span><span class="n">getRootVal</span><span class="p">()]</span>
        <span class="k">return</span> <span class="n">fn</span><span class="p">(</span><span class="n">evaluate</span><span class="p">(</span><span class="n">leftC</span><span class="p">),</span><span class="n">evaluate</span><span class="p">(</span><span class="n">rightC</span><span class="p">))</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">parseTree</span><span class="o">.</span><span class="n">getRootVal</span><span class="p">()</span>

<span class="n">evaluate</span><span class="p">(</span><span class="n">pt</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt output_prompt">Out[55]:</div>


<div class="output_text output_subarea output_execute_result">
<pre>45</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Tree-Traversals">Tree Traversals<a class="anchor-link" href="python-data-structures.html#Tree-Traversals">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<ol>
<li>
<strong>preorder</strong>. In a preorder traversal, we visit the root node first, then recursively do a preorder traversal of the left subtree, followed by a recursive preorder traversal of the right subtree.</li>
<li>
<strong>inorder</strong>. In an inorder traversal, we recursively do an inorder traversal on the left subtree, visit the root node, and finally do a recursive inorder traversal of the right subtree.</li>
<li>
<strong>postorder</strong>. In a postorder traversal, we recursively do a postorder traversal of the left subtree and the right subtree followed by a visit to the root node.</li>
</ol>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [56]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">preorder</span><span class="p">(</span><span class="n">tree</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">tree</span><span class="p">:</span>
        <span class="nb">print</span><span class="p">(</span><span class="n">tree</span><span class="o">.</span><span class="n">getRootVal</span><span class="p">())</span>
        <span class="n">preorder</span><span class="p">(</span><span class="n">tree</span><span class="o">.</span><span class="n">getLeftChild</span><span class="p">())</span>
        <span class="n">preorder</span><span class="p">(</span><span class="n">tree</span><span class="o">.</span><span class="n">getRightChild</span><span class="p">())</span>

<span class="n">preorder</span><span class="p">(</span><span class="n">pt</span><span class="p">)</span>        
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>*
+
10
5
3
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [57]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">preorder</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
    <span class="nb">print</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">key</span><span class="p">)</span>
    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">leftChild</span><span class="p">:</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">left</span><span class="o">.</span><span class="n">preorder</span><span class="p">()</span>
    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">rightChild</span><span class="p">:</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">right</span><span class="o">.</span><span class="n">preorder</span><span class="p">()</span>
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [58]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">postorder</span><span class="p">(</span><span class="n">tree</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">tree</span> <span class="o">!=</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">postorder</span><span class="p">(</span><span class="n">tree</span><span class="o">.</span><span class="n">getLeftChild</span><span class="p">())</span>
        <span class="n">postorder</span><span class="p">(</span><span class="n">tree</span><span class="o">.</span><span class="n">getRightChild</span><span class="p">())</span>
        <span class="nb">print</span><span class="p">(</span><span class="n">tree</span><span class="o">.</span><span class="n">getRootVal</span><span class="p">())</span>

<span class="n">postorder</span><span class="p">(</span><span class="n">pt</span><span class="p">)</span>        
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>10
5
+
3
*
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [59]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">postordereval</span><span class="p">(</span><span class="n">tree</span><span class="p">):</span>
    <span class="n">opers</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'+'</span><span class="p">:</span><span class="n">operator</span><span class="o">.</span><span class="n">add</span><span class="p">,</span> <span class="s1">'-'</span><span class="p">:</span><span class="n">operator</span><span class="o">.</span><span class="n">sub</span><span class="p">,</span> <span class="s1">'*'</span><span class="p">:</span><span class="n">operator</span><span class="o">.</span><span class="n">mul</span><span class="p">,</span> <span class="s1">'/'</span><span class="p">:</span><span class="n">operator</span><span class="o">.</span><span class="n">truediv</span><span class="p">}</span>
    <span class="n">res1</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">res2</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="k">if</span> <span class="n">tree</span><span class="p">:</span>
        <span class="n">res1</span> <span class="o">=</span> <span class="n">postordereval</span><span class="p">(</span><span class="n">tree</span><span class="o">.</span><span class="n">getLeftChild</span><span class="p">())</span>
        <span class="n">res2</span> <span class="o">=</span> <span class="n">postordereval</span><span class="p">(</span><span class="n">tree</span><span class="o">.</span><span class="n">getRightChild</span><span class="p">())</span>
        <span class="k">if</span> <span class="n">res1</span> <span class="ow">and</span> <span class="n">res2</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">opers</span><span class="p">[</span><span class="n">tree</span><span class="o">.</span><span class="n">getRootVal</span><span class="p">()](</span><span class="n">res1</span><span class="p">,</span><span class="n">res2</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">tree</span><span class="o">.</span><span class="n">getRootVal</span><span class="p">()</span>
        
<span class="n">postordereval</span><span class="p">(</span><span class="n">pt</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt output_prompt">Out[59]:</div>


<div class="output_text output_subarea output_execute_result">
<pre>45</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [60]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">inorder</span><span class="p">(</span><span class="n">tree</span><span class="p">):</span>
  <span class="k">if</span> <span class="n">tree</span> <span class="o">!=</span> <span class="kc">None</span><span class="p">:</span>
      <span class="n">inorder</span><span class="p">(</span><span class="n">tree</span><span class="o">.</span><span class="n">getLeftChild</span><span class="p">())</span>
      <span class="nb">print</span><span class="p">(</span><span class="n">tree</span><span class="o">.</span><span class="n">getRootVal</span><span class="p">())</span>
      <span class="n">inorder</span><span class="p">(</span><span class="n">tree</span><span class="o">.</span><span class="n">getRightChild</span><span class="p">())</span>

<span class="n">inorder</span><span class="p">(</span><span class="n">pt</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>10
+
5
*
3
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [61]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">printexp</span><span class="p">(</span><span class="n">tree</span><span class="p">):</span>
  <span class="n">sVal</span> <span class="o">=</span> <span class="s2">""</span>
  <span class="k">if</span> <span class="n">tree</span><span class="p">:</span>
      <span class="n">sVal</span> <span class="o">=</span> <span class="s1">'('</span> <span class="o">+</span> <span class="n">printexp</span><span class="p">(</span><span class="n">tree</span><span class="o">.</span><span class="n">getLeftChild</span><span class="p">())</span>
      <span class="n">sVal</span> <span class="o">=</span> <span class="n">sVal</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">tree</span><span class="o">.</span><span class="n">getRootVal</span><span class="p">())</span>
      <span class="n">sVal</span> <span class="o">=</span> <span class="n">sVal</span> <span class="o">+</span> <span class="n">printexp</span><span class="p">(</span><span class="n">tree</span><span class="o">.</span><span class="n">getRightChild</span><span class="p">())</span><span class="o">+</span><span class="s1">')'</span>
  <span class="k">return</span> <span class="n">sVal</span>

<span class="n">printexp</span><span class="p">(</span><span class="n">pt</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt output_prompt">Out[61]:</div>


<div class="output_text output_subarea output_execute_result">
<pre>'(((10)+(5))*(3))'</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Priority-Queues-with-Binary-Heaps">Priority Queues with Binary Heaps<a class="anchor-link" href="python-data-structures.html#Priority-Queues-with-Binary-Heaps">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>A <strong>priority queue</strong> acts like a queue in that you dequeue an item by removing it from the front. However, in a priority queue the logical order of items inside a queue is determined by their priority. The highest priority items are at the front of the queue and the lowest priority items are at the back. Thus when you enqueue an item on a priority queue, the new item may move all the way to the front.</p>
<p>The classic way to implement a priority queue is using a data structure called a <strong>binary heap</strong>. A binary heap will allow us both enqueue and dequeue items in $O(logn)$.</p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Binary-Heap-Operations">Binary Heap Operations<a class="anchor-link" href="python-data-structures.html#Binary-Heap-Operations">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<ul>
<li>BinaryHeap() creates a new, empty, binary heap.</li>
<li>insert(k) adds a new item to the heap.</li>
<li>findMin() returns the item with the minimum key value, leaving item in the heap.</li>
<li>delMin() returns the item with the minimum key value, removing the item from the heap.</li>
<li>isEmpty() returns true if the heap is empty, false otherwise.</li>
<li>size() returns the number of items in the heap.</li>
<li>buildHeap(list) builds a new heap from a list of keys.</li>
</ul>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [62]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">pythonds.trees.binheap</span> <span class="k">import</span> <span class="n">BinHeap</span>

<span class="n">bh</span> <span class="o">=</span> <span class="n">BinHeap</span><span class="p">()</span>
<span class="n">bh</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
<span class="n">bh</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span>
<span class="n">bh</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="n">bh</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">11</span><span class="p">)</span>

<span class="nb">print</span><span class="p">(</span><span class="n">bh</span><span class="o">.</span><span class="n">delMin</span><span class="p">())</span>
<span class="nb">print</span><span class="p">(</span><span class="n">bh</span><span class="o">.</span><span class="n">delMin</span><span class="p">())</span>
<span class="nb">print</span><span class="p">(</span><span class="n">bh</span><span class="o">.</span><span class="n">delMin</span><span class="p">())</span>
<span class="nb">print</span><span class="p">(</span><span class="n">bh</span><span class="o">.</span><span class="n">delMin</span><span class="p">())</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>3
5
7
11
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Binary-Heap-Implementation">Binary Heap Implementation<a class="anchor-link" href="python-data-structures.html#Binary-Heap-Implementation">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [63]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">class</span> <span class="nc">BinHeap</span><span class="p">:</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">heapList</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">currentSize</span> <span class="o">=</span> <span class="mi">0</span>


    <span class="k">def</span> <span class="nf">percUp</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">i</span><span class="p">):</span>
        <span class="k">while</span> <span class="n">i</span> <span class="o">//</span> <span class="mi">2</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
          <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">heapList</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">heapList</span><span class="p">[</span><span class="n">i</span> <span class="o">//</span> <span class="mi">2</span><span class="p">]:</span>
             <span class="n">tmp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">heapList</span><span class="p">[</span><span class="n">i</span> <span class="o">//</span> <span class="mi">2</span><span class="p">]</span>
             <span class="bp">self</span><span class="o">.</span><span class="n">heapList</span><span class="p">[</span><span class="n">i</span> <span class="o">//</span> <span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">heapList</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
             <span class="bp">self</span><span class="o">.</span><span class="n">heapList</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">tmp</span>
          <span class="n">i</span> <span class="o">=</span> <span class="n">i</span> <span class="o">//</span> <span class="mi">2</span>

    <span class="k">def</span> <span class="nf">insert</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">k</span><span class="p">):</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">heapList</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">k</span><span class="p">)</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">currentSize</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">currentSize</span> <span class="o">+</span> <span class="mi">1</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">percUp</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">currentSize</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">percDown</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">i</span><span class="p">):</span>
      <span class="k">while</span> <span class="p">(</span><span class="n">i</span> <span class="o">*</span> <span class="mi">2</span><span class="p">)</span> <span class="o">&lt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">currentSize</span><span class="p">:</span>
          <span class="n">mc</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">minChild</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
          <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">heapList</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">heapList</span><span class="p">[</span><span class="n">mc</span><span class="p">]:</span>
              <span class="n">tmp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">heapList</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
              <span class="bp">self</span><span class="o">.</span><span class="n">heapList</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">heapList</span><span class="p">[</span><span class="n">mc</span><span class="p">]</span>
              <span class="bp">self</span><span class="o">.</span><span class="n">heapList</span><span class="p">[</span><span class="n">mc</span><span class="p">]</span> <span class="o">=</span> <span class="n">tmp</span>
          <span class="n">i</span> <span class="o">=</span> <span class="n">mc</span>

    <span class="k">def</span> <span class="nf">minChild</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">i</span><span class="p">):</span>
      <span class="k">if</span> <span class="n">i</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">currentSize</span><span class="p">:</span>
          <span class="k">return</span> <span class="n">i</span> <span class="o">*</span> <span class="mi">2</span>
      <span class="k">else</span><span class="p">:</span>
          <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">heapList</span><span class="p">[</span><span class="n">i</span><span class="o">*</span><span class="mi">2</span><span class="p">]</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">heapList</span><span class="p">[</span><span class="n">i</span><span class="o">*</span><span class="mi">2</span><span class="o">+</span><span class="mi">1</span><span class="p">]:</span>
              <span class="k">return</span> <span class="n">i</span> <span class="o">*</span> <span class="mi">2</span>
          <span class="k">else</span><span class="p">:</span>
              <span class="k">return</span> <span class="n">i</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span>

    <span class="k">def</span> <span class="nf">delMin</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
      <span class="n">retval</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">heapList</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">heapList</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">heapList</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">currentSize</span><span class="p">]</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">currentSize</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">currentSize</span> <span class="o">-</span> <span class="mi">1</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">heapList</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">percDown</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
      <span class="k">return</span> <span class="n">retval</span>

    <span class="k">def</span> <span class="nf">buildHeap</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">alist</span><span class="p">):</span>
      <span class="n">i</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">alist</span><span class="p">)</span> <span class="o">//</span> <span class="mi">2</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">currentSize</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">alist</span><span class="p">)</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">heapList</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="n">alist</span><span class="p">[:]</span>
      <span class="k">while</span> <span class="p">(</span><span class="n">i</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">):</span>
          <span class="bp">self</span><span class="o">.</span><span class="n">percDown</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
          <span class="n">i</span> <span class="o">=</span> <span class="n">i</span> <span class="o">-</span> <span class="mi">1</span>

<span class="n">bh</span> <span class="o">=</span> <span class="n">BinHeap</span><span class="p">()</span>
<span class="n">bh</span><span class="o">.</span><span class="n">buildHeap</span><span class="p">([</span><span class="mi">9</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">])</span>

<span class="nb">print</span><span class="p">(</span><span class="n">bh</span><span class="o">.</span><span class="n">delMin</span><span class="p">())</span>
<span class="nb">print</span><span class="p">(</span><span class="n">bh</span><span class="o">.</span><span class="n">delMin</span><span class="p">())</span>
<span class="nb">print</span><span class="p">(</span><span class="n">bh</span><span class="o">.</span><span class="n">delMin</span><span class="p">())</span>
<span class="nb">print</span><span class="p">(</span><span class="n">bh</span><span class="o">.</span><span class="n">delMin</span><span class="p">())</span>
<span class="nb">print</span><span class="p">(</span><span class="n">bh</span><span class="o">.</span><span class="n">delMin</span><span class="p">())</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>2
3
5
6
9
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Binary-Search-Tree-Operations">Binary Search Tree Operations<a class="anchor-link" href="python-data-structures.html#Binary-Search-Tree-Operations">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<ul>
<li>Map() Create a new, empty map.</li>
<li>put(key,val) Add a new key-value pair to the map. If the key is already in the map then replace the old value with the new value.</li>
<li>get(key) Given a key, return the value stored in the map or None otherwise.</li>
<li>del Delete the key-value pair from the map using a statement of the form del map[key].</li>
<li>len() Return the number of key-value pairs stored in the map.</li>
<li>in Return True for a statement of the form key in map, if the given key is in the map.</li>
</ul>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [64]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">class</span> <span class="nc">TreeNode</span><span class="p">:</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">key</span><span class="p">,</span><span class="n">val</span><span class="p">,</span><span class="n">left</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span><span class="n">right</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span><span class="n">parent</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">key</span> <span class="o">=</span> <span class="n">key</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">payload</span> <span class="o">=</span> <span class="n">val</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">leftChild</span> <span class="o">=</span> <span class="n">left</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">rightChild</span> <span class="o">=</span> <span class="n">right</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">parent</span> <span class="o">=</span> <span class="n">parent</span>

    <span class="k">def</span> <span class="nf">hasLeftChild</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">leftChild</span>

    <span class="k">def</span> <span class="nf">hasRightChild</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">rightChild</span>

    <span class="k">def</span> <span class="nf">isLeftChild</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">parent</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">leftChild</span> <span class="o">==</span> <span class="bp">self</span>

    <span class="k">def</span> <span class="nf">isRightChild</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">parent</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">rightChild</span> <span class="o">==</span> <span class="bp">self</span>

    <span class="k">def</span> <span class="nf">isRoot</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">parent</span>

    <span class="k">def</span> <span class="nf">isLeaf</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="ow">not</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rightChild</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">leftChild</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">hasAnyChildren</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">rightChild</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">leftChild</span>

    <span class="k">def</span> <span class="nf">hasBothChildren</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">rightChild</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">leftChild</span>

    <span class="k">def</span> <span class="nf">replaceNodeData</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">key</span><span class="p">,</span><span class="n">value</span><span class="p">,</span><span class="n">lc</span><span class="p">,</span><span class="n">rc</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">key</span> <span class="o">=</span> <span class="n">key</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">payload</span> <span class="o">=</span> <span class="n">value</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">leftChild</span> <span class="o">=</span> <span class="n">lc</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">rightChild</span> <span class="o">=</span> <span class="n">rc</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">hasLeftChild</span><span class="p">():</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">leftChild</span><span class="o">.</span><span class="n">parent</span> <span class="o">=</span> <span class="bp">self</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">hasRightChild</span><span class="p">():</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">rightChild</span><span class="o">.</span><span class="n">parent</span> <span class="o">=</span> <span class="bp">self</span>


<span class="k">class</span> <span class="nc">BinarySearchTree</span><span class="p">:</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">root</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">size</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="k">def</span> <span class="nf">length</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">size</span>

    <span class="k">def</span> <span class="nf">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">size</span>

    <span class="k">def</span> <span class="nf">put</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">key</span><span class="p">,</span><span class="n">val</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">root</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_put</span><span class="p">(</span><span class="n">key</span><span class="p">,</span><span class="n">val</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">root</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">root</span> <span class="o">=</span> <span class="n">TreeNode</span><span class="p">(</span><span class="n">key</span><span class="p">,</span><span class="n">val</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">size</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">size</span> <span class="o">+</span> <span class="mi">1</span>

    <span class="k">def</span> <span class="nf">_put</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">key</span><span class="p">,</span><span class="n">val</span><span class="p">,</span><span class="n">currentNode</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">key</span> <span class="o">&lt;</span> <span class="n">currentNode</span><span class="o">.</span><span class="n">key</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">currentNode</span><span class="o">.</span><span class="n">hasLeftChild</span><span class="p">():</span>
                   <span class="bp">self</span><span class="o">.</span><span class="n">_put</span><span class="p">(</span><span class="n">key</span><span class="p">,</span><span class="n">val</span><span class="p">,</span><span class="n">currentNode</span><span class="o">.</span><span class="n">leftChild</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                   <span class="n">currentNode</span><span class="o">.</span><span class="n">leftChild</span> <span class="o">=</span> <span class="n">TreeNode</span><span class="p">(</span><span class="n">key</span><span class="p">,</span><span class="n">val</span><span class="p">,</span><span class="n">parent</span><span class="o">=</span><span class="n">currentNode</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">currentNode</span><span class="o">.</span><span class="n">hasRightChild</span><span class="p">():</span>
                   <span class="bp">self</span><span class="o">.</span><span class="n">_put</span><span class="p">(</span><span class="n">key</span><span class="p">,</span><span class="n">val</span><span class="p">,</span><span class="n">currentNode</span><span class="o">.</span><span class="n">rightChild</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                   <span class="n">currentNode</span><span class="o">.</span><span class="n">rightChild</span> <span class="o">=</span> <span class="n">TreeNode</span><span class="p">(</span><span class="n">key</span><span class="p">,</span><span class="n">val</span><span class="p">,</span><span class="n">parent</span><span class="o">=</span><span class="n">currentNode</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__setitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">k</span><span class="p">,</span><span class="n">v</span><span class="p">):</span>
       <span class="bp">self</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="n">k</span><span class="p">,</span><span class="n">v</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">key</span><span class="p">):</span>
       <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">root</span><span class="p">:</span>
           <span class="n">res</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get</span><span class="p">(</span><span class="n">key</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">root</span><span class="p">)</span>
           <span class="k">if</span> <span class="n">res</span><span class="p">:</span>
                  <span class="k">return</span> <span class="n">res</span><span class="o">.</span><span class="n">payload</span>
           <span class="k">else</span><span class="p">:</span>
                  <span class="k">return</span> <span class="kc">None</span>
       <span class="k">else</span><span class="p">:</span>
           <span class="k">return</span> <span class="kc">None</span>

    <span class="k">def</span> <span class="nf">_get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">key</span><span class="p">,</span><span class="n">currentNode</span><span class="p">):</span>
       <span class="k">if</span> <span class="ow">not</span> <span class="n">currentNode</span><span class="p">:</span>
           <span class="k">return</span> <span class="kc">None</span>
       <span class="k">elif</span> <span class="n">currentNode</span><span class="o">.</span><span class="n">key</span> <span class="o">==</span> <span class="n">key</span><span class="p">:</span>
           <span class="k">return</span> <span class="n">currentNode</span>
       <span class="k">elif</span> <span class="n">key</span> <span class="o">&lt;</span> <span class="n">currentNode</span><span class="o">.</span><span class="n">key</span><span class="p">:</span>
           <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get</span><span class="p">(</span><span class="n">key</span><span class="p">,</span><span class="n">currentNode</span><span class="o">.</span><span class="n">leftChild</span><span class="p">)</span>
       <span class="k">else</span><span class="p">:</span>
           <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get</span><span class="p">(</span><span class="n">key</span><span class="p">,</span><span class="n">currentNode</span><span class="o">.</span><span class="n">rightChild</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">key</span><span class="p">):</span>
       <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__contains__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">key</span><span class="p">):</span>
       <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get</span><span class="p">(</span><span class="n">key</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">root</span><span class="p">):</span>
           <span class="k">return</span> <span class="kc">True</span>
       <span class="k">else</span><span class="p">:</span>
           <span class="k">return</span> <span class="kc">False</span>

    <span class="k">def</span> <span class="nf">delete</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">key</span><span class="p">):</span>
      <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">size</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
         <span class="n">nodeToRemove</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get</span><span class="p">(</span><span class="n">key</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">root</span><span class="p">)</span>
         <span class="k">if</span> <span class="n">nodeToRemove</span><span class="p">:</span>
             <span class="bp">self</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">nodeToRemove</span><span class="p">)</span>
             <span class="bp">self</span><span class="o">.</span><span class="n">size</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">size</span><span class="o">-</span><span class="mi">1</span>
         <span class="k">else</span><span class="p">:</span>
             <span class="k">raise</span> <span class="ne">KeyError</span><span class="p">(</span><span class="s1">'Error, key not in tree'</span><span class="p">)</span>
      <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">size</span> <span class="o">==</span> <span class="mi">1</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">root</span><span class="o">.</span><span class="n">key</span> <span class="o">==</span> <span class="n">key</span><span class="p">:</span>
         <span class="bp">self</span><span class="o">.</span><span class="n">root</span> <span class="o">=</span> <span class="kc">None</span>
         <span class="bp">self</span><span class="o">.</span><span class="n">size</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">size</span> <span class="o">-</span> <span class="mi">1</span>
      <span class="k">else</span><span class="p">:</span>
         <span class="k">raise</span> <span class="ne">KeyError</span><span class="p">(</span><span class="s1">'Error, key not in tree'</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__delitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">key</span><span class="p">):</span>
       <span class="bp">self</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">spliceOut</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
       <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">isLeaf</span><span class="p">():</span>
           <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">isLeftChild</span><span class="p">():</span>
                  <span class="bp">self</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">leftChild</span> <span class="o">=</span> <span class="kc">None</span>
           <span class="k">else</span><span class="p">:</span>
                  <span class="bp">self</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">rightChild</span> <span class="o">=</span> <span class="kc">None</span>
       <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">hasAnyChildren</span><span class="p">():</span>
           <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">hasLeftChild</span><span class="p">():</span>
                  <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">isLeftChild</span><span class="p">():</span>
                     <span class="bp">self</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">leftChild</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">leftChild</span>
                  <span class="k">else</span><span class="p">:</span>
                     <span class="bp">self</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">rightChild</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">leftChild</span>
                  <span class="bp">self</span><span class="o">.</span><span class="n">leftChild</span><span class="o">.</span><span class="n">parent</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">parent</span>
           <span class="k">else</span><span class="p">:</span>
                  <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">isLeftChild</span><span class="p">():</span>
                     <span class="bp">self</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">leftChild</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rightChild</span>
                  <span class="k">else</span><span class="p">:</span>
                     <span class="bp">self</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">rightChild</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rightChild</span>
                  <span class="bp">self</span><span class="o">.</span><span class="n">rightChild</span><span class="o">.</span><span class="n">parent</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">parent</span>

    <span class="k">def</span> <span class="nf">findSuccessor</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
      <span class="n">succ</span> <span class="o">=</span> <span class="kc">None</span>
      <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">hasRightChild</span><span class="p">():</span>
          <span class="n">succ</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rightChild</span><span class="o">.</span><span class="n">findMin</span><span class="p">()</span>
      <span class="k">else</span><span class="p">:</span>
          <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">parent</span><span class="p">:</span>
                 <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">isLeftChild</span><span class="p">():</span>
                     <span class="n">succ</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">parent</span>
                 <span class="k">else</span><span class="p">:</span>
                     <span class="bp">self</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">rightChild</span> <span class="o">=</span> <span class="kc">None</span>
                     <span class="n">succ</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">findSuccessor</span><span class="p">()</span>
                     <span class="bp">self</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">rightChild</span> <span class="o">=</span> <span class="bp">self</span>
      <span class="k">return</span> <span class="n">succ</span>

    <span class="k">def</span> <span class="nf">findMin</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
      <span class="n">current</span> <span class="o">=</span> <span class="bp">self</span>
      <span class="k">while</span> <span class="n">current</span><span class="o">.</span><span class="n">hasLeftChild</span><span class="p">():</span>
          <span class="n">current</span> <span class="o">=</span> <span class="n">current</span><span class="o">.</span><span class="n">leftChild</span>
      <span class="k">return</span> <span class="n">current</span>

    <span class="k">def</span> <span class="nf">remove</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">currentNode</span><span class="p">):</span>
         <span class="k">if</span> <span class="n">currentNode</span><span class="o">.</span><span class="n">isLeaf</span><span class="p">():</span> <span class="c1">#leaf</span>
           <span class="k">if</span> <span class="n">currentNode</span> <span class="o">==</span> <span class="n">currentNode</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">leftChild</span><span class="p">:</span>
               <span class="n">currentNode</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">leftChild</span> <span class="o">=</span> <span class="kc">None</span>
           <span class="k">else</span><span class="p">:</span>
               <span class="n">currentNode</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">rightChild</span> <span class="o">=</span> <span class="kc">None</span>
         <span class="k">elif</span> <span class="n">currentNode</span><span class="o">.</span><span class="n">hasBothChildren</span><span class="p">():</span> <span class="c1">#interior</span>
           <span class="n">succ</span> <span class="o">=</span> <span class="n">currentNode</span><span class="o">.</span><span class="n">findSuccessor</span><span class="p">()</span>
           <span class="n">succ</span><span class="o">.</span><span class="n">spliceOut</span><span class="p">()</span>
           <span class="n">currentNode</span><span class="o">.</span><span class="n">key</span> <span class="o">=</span> <span class="n">succ</span><span class="o">.</span><span class="n">key</span>
           <span class="n">currentNode</span><span class="o">.</span><span class="n">payload</span> <span class="o">=</span> <span class="n">succ</span><span class="o">.</span><span class="n">payload</span>

         <span class="k">else</span><span class="p">:</span> <span class="c1"># this node has one child</span>
           <span class="k">if</span> <span class="n">currentNode</span><span class="o">.</span><span class="n">hasLeftChild</span><span class="p">():</span>
             <span class="k">if</span> <span class="n">currentNode</span><span class="o">.</span><span class="n">isLeftChild</span><span class="p">():</span>
                 <span class="n">currentNode</span><span class="o">.</span><span class="n">leftChild</span><span class="o">.</span><span class="n">parent</span> <span class="o">=</span> <span class="n">currentNode</span><span class="o">.</span><span class="n">parent</span>
                 <span class="n">currentNode</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">leftChild</span> <span class="o">=</span> <span class="n">currentNode</span><span class="o">.</span><span class="n">leftChild</span>
             <span class="k">elif</span> <span class="n">currentNode</span><span class="o">.</span><span class="n">isRightChild</span><span class="p">():</span>
                 <span class="n">currentNode</span><span class="o">.</span><span class="n">leftChild</span><span class="o">.</span><span class="n">parent</span> <span class="o">=</span> <span class="n">currentNode</span><span class="o">.</span><span class="n">parent</span>
                 <span class="n">currentNode</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">rightChild</span> <span class="o">=</span> <span class="n">currentNode</span><span class="o">.</span><span class="n">leftChild</span>
             <span class="k">else</span><span class="p">:</span>
                 <span class="n">currentNode</span><span class="o">.</span><span class="n">replaceNodeData</span><span class="p">(</span><span class="n">currentNode</span><span class="o">.</span><span class="n">leftChild</span><span class="o">.</span><span class="n">key</span><span class="p">,</span>
                                    <span class="n">currentNode</span><span class="o">.</span><span class="n">leftChild</span><span class="o">.</span><span class="n">payload</span><span class="p">,</span>
                                    <span class="n">currentNode</span><span class="o">.</span><span class="n">leftChild</span><span class="o">.</span><span class="n">leftChild</span><span class="p">,</span>
                                    <span class="n">currentNode</span><span class="o">.</span><span class="n">leftChild</span><span class="o">.</span><span class="n">rightChild</span><span class="p">)</span>
           <span class="k">else</span><span class="p">:</span>
             <span class="k">if</span> <span class="n">currentNode</span><span class="o">.</span><span class="n">isLeftChild</span><span class="p">():</span>
                 <span class="n">currentNode</span><span class="o">.</span><span class="n">rightChild</span><span class="o">.</span><span class="n">parent</span> <span class="o">=</span> <span class="n">currentNode</span><span class="o">.</span><span class="n">parent</span>
                 <span class="n">currentNode</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">leftChild</span> <span class="o">=</span> <span class="n">currentNode</span><span class="o">.</span><span class="n">rightChild</span>
             <span class="k">elif</span> <span class="n">currentNode</span><span class="o">.</span><span class="n">isRightChild</span><span class="p">():</span>
                 <span class="n">currentNode</span><span class="o">.</span><span class="n">rightChild</span><span class="o">.</span><span class="n">parent</span> <span class="o">=</span> <span class="n">currentNode</span><span class="o">.</span><span class="n">parent</span>
                 <span class="n">currentNode</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">rightChild</span> <span class="o">=</span> <span class="n">currentNode</span><span class="o">.</span><span class="n">rightChild</span>
             <span class="k">else</span><span class="p">:</span>
                 <span class="n">currentNode</span><span class="o">.</span><span class="n">replaceNodeData</span><span class="p">(</span><span class="n">currentNode</span><span class="o">.</span><span class="n">rightChild</span><span class="o">.</span><span class="n">key</span><span class="p">,</span>
                                    <span class="n">currentNode</span><span class="o">.</span><span class="n">rightChild</span><span class="o">.</span><span class="n">payload</span><span class="p">,</span>
                                    <span class="n">currentNode</span><span class="o">.</span><span class="n">rightChild</span><span class="o">.</span><span class="n">leftChild</span><span class="p">,</span>
                                    <span class="n">currentNode</span><span class="o">.</span><span class="n">rightChild</span><span class="o">.</span><span class="n">rightChild</span><span class="p">)</span>




<span class="n">mytree</span> <span class="o">=</span> <span class="n">BinarySearchTree</span><span class="p">()</span>
<span class="n">mytree</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span><span class="o">=</span><span class="s2">"red"</span>
<span class="n">mytree</span><span class="p">[</span><span class="mi">4</span><span class="p">]</span><span class="o">=</span><span class="s2">"blue"</span>
<span class="n">mytree</span><span class="p">[</span><span class="mi">6</span><span class="p">]</span><span class="o">=</span><span class="s2">"yellow"</span>
<span class="n">mytree</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span><span class="o">=</span><span class="s2">"at"</span>

<span class="nb">print</span><span class="p">(</span><span class="n">mytree</span><span class="p">[</span><span class="mi">6</span><span class="p">])</span>
<span class="nb">print</span><span class="p">(</span><span class="n">mytree</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>yellow
at
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Balanced-Binary-Search-Trees">Balanced Binary Search Trees<a class="anchor-link" href="python-data-structures.html#Balanced-Binary-Search-Trees">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>A special kind of binary search tree that automatically makes sure that the tree remains balanced at all times. This tree is called an <strong>AVL tree</strong> and is named for its inventors: G.M. Adelson-Velskii and E.M. Landis.</p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>An <strong>AVL tree</strong> implements the Map abstract data type just like a regular binary search tree, the only difference is in how the tree performs. To implement our <strong>AVL tree</strong> we need to keep track of a <strong>balance factor</strong> for each node in the tree. We do this by looking at the heights of the left and right subtrees for each node. More formally, we define the balance factor for a node as the difference between the height of the left subtree and the height of the right subtree.</p>
$$balanceFactor=height(leftSubTree)−height(rightSubTree)$$<p>Using the definition for balance factor given above we say that a subtree is left-heavy if the balance factor is greater than zero. If the balance factor is less than zero then the subtree is right heavy. If the balance factor is zero then the tree is perfectly in balance. For purposes of implementing an <strong>AVL tree</strong>, and gaining the benefit of having a balanced tree we will define a tree to be in balance if the balance factor is -1, 0, or 1. Once the balance factor of a node in a tree is outside this range we will need to have a procedure to bring the tree back into balance.</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [65]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">_put</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">val</span><span class="p">,</span> <span class="n">currentNode</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">key</span> <span class="o">&lt;</span> <span class="n">currentNode</span><span class="o">.</span><span class="n">key</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">currentNode</span><span class="o">.</span><span class="n">hasLeftChild</span><span class="p">():</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_put</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">val</span><span class="p">,</span> <span class="n">currentNode</span><span class="o">.</span><span class="n">leftChild</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">currentNode</span><span class="o">.</span><span class="n">leftChild</span> <span class="o">=</span> <span class="n">TreeNode</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">val</span><span class="p">,</span> <span class="n">parent</span><span class="o">=</span><span class="n">currentNode</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">updateBalance</span><span class="p">(</span><span class="n">currentNode</span><span class="o">.</span><span class="n">leftChild</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">currentNode</span><span class="o">.</span><span class="n">hasRightChild</span><span class="p">():</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_put</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">val</span><span class="p">,</span> <span class="n">currentNode</span><span class="o">.</span><span class="n">rightChild</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">currentNode</span><span class="o">.</span><span class="n">rightChild</span> <span class="o">=</span> <span class="n">TreeNode</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">val</span><span class="p">,</span> <span class="n">parent</span><span class="o">=</span><span class="n">currentNode</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">updateBalance</span><span class="p">(</span><span class="n">currentNode</span><span class="o">.</span><span class="n">rightChild</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">updateBalance</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">node</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">node</span><span class="o">.</span><span class="n">balanceFactor</span> <span class="o">&gt;</span> <span class="mi">1</span> <span class="ow">or</span> <span class="n">node</span><span class="o">.</span><span class="n">balanceFactor</span> <span class="o">&lt;</span> <span class="o">-</span><span class="mi">1</span><span class="p">:</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">rebalance</span><span class="p">(</span><span class="n">node</span><span class="p">)</span>
        <span class="k">return</span>
    <span class="k">if</span> <span class="n">node</span><span class="o">.</span><span class="n">parent</span> <span class="o">!=</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">node</span><span class="o">.</span><span class="n">isLeftChild</span><span class="p">():</span>
            <span class="n">node</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">balanceFactor</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="k">elif</span> <span class="n">node</span><span class="o">.</span><span class="n">isRightChild</span><span class="p">():</span>
            <span class="n">node</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">balanceFactor</span> <span class="o">-=</span> <span class="mi">1</span>

        <span class="k">if</span> <span class="n">node</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">balanceFactor</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">updateBalance</span><span class="p">(</span><span class="n">node</span><span class="o">.</span><span class="n">parent</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">rotateLeft</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">rotRoot</span><span class="p">):</span>
    <span class="n">newRoot</span> <span class="o">=</span> <span class="n">rotRoot</span><span class="o">.</span><span class="n">rightChild</span>
    <span class="n">rotRoot</span><span class="o">.</span><span class="n">rightChild</span> <span class="o">=</span> <span class="n">newRoot</span><span class="o">.</span><span class="n">leftChild</span>
    <span class="k">if</span> <span class="n">newRoot</span><span class="o">.</span><span class="n">leftChild</span> <span class="o">!=</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">newRoot</span><span class="o">.</span><span class="n">leftChild</span><span class="o">.</span><span class="n">parent</span> <span class="o">=</span> <span class="n">rotRoot</span>
    <span class="n">newRoot</span><span class="o">.</span><span class="n">parent</span> <span class="o">=</span> <span class="n">rotRoot</span><span class="o">.</span><span class="n">parent</span>
    <span class="k">if</span> <span class="n">rotRoot</span><span class="o">.</span><span class="n">isRoot</span><span class="p">():</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">root</span> <span class="o">=</span> <span class="n">newRoot</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">rotRoot</span><span class="o">.</span><span class="n">isLeftChild</span><span class="p">():</span>
            <span class="n">rotRoot</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">leftChild</span> <span class="o">=</span> <span class="n">newRoot</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">rotRoot</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">rightChild</span> <span class="o">=</span> <span class="n">newRoot</span>
    <span class="n">newRoot</span><span class="o">.</span><span class="n">leftChild</span> <span class="o">=</span> <span class="n">rotRoot</span>
    <span class="n">rotRoot</span><span class="o">.</span><span class="n">parent</span> <span class="o">=</span> <span class="n">newRoot</span>
    <span class="n">rotRoot</span><span class="o">.</span><span class="n">balanceFactor</span> <span class="o">=</span> <span class="n">rotRoot</span><span class="o">.</span><span class="n">balanceFactor</span> <span class="o">+</span> \
        <span class="mi">1</span> <span class="o">-</span> <span class="nb">min</span><span class="p">(</span><span class="n">newRoot</span><span class="o">.</span><span class="n">balanceFactor</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
    <span class="n">newRoot</span><span class="o">.</span><span class="n">balanceFactor</span> <span class="o">=</span> <span class="n">newRoot</span><span class="o">.</span><span class="n">balanceFactor</span> <span class="o">+</span> \
        <span class="mi">1</span> <span class="o">+</span> <span class="nb">max</span><span class="p">(</span><span class="n">rotRoot</span><span class="o">.</span><span class="n">balanceFactor</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">rebalance</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">node</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">node</span><span class="o">.</span><span class="n">balanceFactor</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">node</span><span class="o">.</span><span class="n">rightChild</span><span class="o">.</span><span class="n">balanceFactor</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">rotateRight</span><span class="p">(</span><span class="n">node</span><span class="o">.</span><span class="n">rightChild</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">rotateLeft</span><span class="p">(</span><span class="n">node</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">rotateLeft</span><span class="p">(</span><span class="n">node</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">node</span><span class="o">.</span><span class="n">balanceFactor</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">node</span><span class="o">.</span><span class="n">leftChild</span><span class="o">.</span><span class="n">balanceFactor</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">rotateLeft</span><span class="p">(</span><span class="n">node</span><span class="o">.</span><span class="n">leftChild</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">rotateRight</span><span class="p">(</span><span class="n">node</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">rotateRight</span><span class="p">(</span><span class="n">node</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Summary-of-Map-ADT-Implementations">Summary of Map ADT Implementations<a class="anchor-link" href="python-data-structures.html#Summary-of-Map-ADT-Implementations">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<table border="0" class="table" id="tab-compare">
<caption><strong>Table 1: Comparing the Performance of Different Map Implementations</strong></caption>
<colgroup>
<col width="13%">
<col width="27%">
<col width="14%">
<col width="22%">
<col width="24%">
</colgroup>
<thead valign="bottom"><tr class="row-odd">
<th class="head">operation</th>
<th class="head">Sorted List</th>
<th class="head">Hash Table</th>
<th class="head">Binary Search Tree</th>
<th class="head">AVL Tree</th>
</tr></thead>
<tbody valign="top">
<tr class="row-even">
<td>put</td>
<td><span class="math"><span class="MathJax_Preview"></span><span class="MathJax" id="MathJax-Element-1-Frame" role="textbox" aria-readonly="true"><nobr><span class="math" id="MathJax-Span-1" style="width: 2.638em; display: inline-block;"><span style="display: inline-block; position: relative; width: 2.169em; height: 0px; font-size: 122%;"><span style="position: absolute; clip: rect(1.759em 1000.003em 3.106em -0.465em); top: -2.69em; left: 0.003em;"><span class="mrow" id="MathJax-Span-2"><span class="mi" id="MathJax-Span-3" style="font-family: MathJax_Math; font-style: italic;">O</span><span class="mo" id="MathJax-Span-4" style="font-family: MathJax_Main;">(</span><span class="mi" id="MathJax-Span-5" style="font-family: MathJax_Math; font-style: italic;">n</span><span class="mo" id="MathJax-Span-6" style="font-family: MathJax_Main;">)</span></span><span style="display: inline-block; width: 0px; height: 2.696em;"></span></span></span><span style="border-left-width: 0.004em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 1.361em; vertical-align: -0.354em;"></span></span></nobr></span><script type="math/tex" id="MathJax-Element-1">O(n)</script></span></td>
<td><span class="math"><span class="MathJax_Preview"></span><span class="MathJax" id="MathJax-Element-2-Frame" role="textbox" aria-readonly="true"><nobr><span class="math" id="MathJax-Span-7" style="width: 2.52em; display: inline-block;"><span style="display: inline-block; position: relative; width: 2.052em; height: 0px; font-size: 122%;"><span style="position: absolute; clip: rect(1.759em 1000.003em 3.106em -0.465em); top: -2.69em; left: 0.003em;"><span class="mrow" id="MathJax-Span-8"><span class="mi" id="MathJax-Span-9" style="font-family: MathJax_Math; font-style: italic;">O</span><span class="mo" id="MathJax-Span-10" style="font-family: MathJax_Main;">(</span><span class="mn" id="MathJax-Span-11" style="font-family: MathJax_Main;">1</span><span class="mo" id="MathJax-Span-12" style="font-family: MathJax_Main;">)</span></span><span style="display: inline-block; width: 0px; height: 2.696em;"></span></span></span><span style="border-left-width: 0.004em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 1.361em; vertical-align: -0.354em;"></span></span></nobr></span><script type="math/tex" id="MathJax-Element-2">O(1)</script></span></td>
<td><span class="math"><span class="MathJax_Preview"></span><span class="MathJax" id="MathJax-Element-3-Frame" role="textbox" aria-readonly="true"><nobr><span class="math" id="MathJax-Span-13" style="width: 2.638em; display: inline-block;"><span style="display: inline-block; position: relative; width: 2.169em; height: 0px; font-size: 122%;"><span style="position: absolute; clip: rect(1.759em 1000.003em 3.106em -0.465em); top: -2.69em; left: 0.003em;"><span class="mrow" id="MathJax-Span-14"><span class="mi" id="MathJax-Span-15" style="font-family: MathJax_Math; font-style: italic;">O</span><span class="mo" id="MathJax-Span-16" style="font-family: MathJax_Main;">(</span><span class="mi" id="MathJax-Span-17" style="font-family: MathJax_Math; font-style: italic;">n</span><span class="mo" id="MathJax-Span-18" style="font-family: MathJax_Main;">)</span></span><span style="display: inline-block; width: 0px; height: 2.696em;"></span></span></span><span style="border-left-width: 0.004em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 1.361em; vertical-align: -0.354em;"></span></span></nobr></span><script type="math/tex" id="MathJax-Element-3">O(n)</script></span></td>
<td><span class="math"><span class="MathJax_Preview"></span><span class="MathJax" id="MathJax-Element-4-Frame" role="textbox" aria-readonly="true"><nobr><span class="math" id="MathJax-Span-19" style="width: 4.921em; display: inline-block;"><span style="display: inline-block; position: relative; width: 4.043em; height: 0px; font-size: 122%;"><span style="position: absolute; clip: rect(1.759em 1000.003em 3.106em -0.465em); top: -2.69em; left: 0.003em;"><span class="mrow" id="MathJax-Span-20"><span class="mi" id="MathJax-Span-21" style="font-family: MathJax_Math; font-style: italic;">O</span><span class="mo" id="MathJax-Span-22" style="font-family: MathJax_Main;">(</span><span class="msubsup" id="MathJax-Span-23"><span style="display: inline-block; position: relative; width: 1.759em; height: 0px;"><span style="position: absolute; clip: rect(1.818em 1000.003em 3.106em -0.524em); top: -2.69em; left: 0.003em;"><span class="mi" id="MathJax-Span-24" style="font-family: MathJax_Main;">log</span><span style="display: inline-block; width: 0px; height: 2.696em;"></span></span><span style="position: absolute; top: -2.046em; left: 1.291em;"><span class="mn" id="MathJax-Span-25" style="font-size: 70.7%; font-family: MathJax_Main;">2</span><span style="display: inline-block; width: 0px; height: 2.286em;"></span></span></span></span><span class="texatom" id="MathJax-Span-26" style="padding-left: 0.179em;"><span class="mrow" id="MathJax-Span-27"><span class="mi" id="MathJax-Span-28" style="font-family: MathJax_Math; font-style: italic;">n</span></span></span><span class="mo" id="MathJax-Span-29" style="font-family: MathJax_Main;">)</span></span><span style="display: inline-block; width: 0px; height: 2.696em;"></span></span></span><span style="border-left-width: 0.004em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 1.361em; vertical-align: -0.354em;"></span></span></nobr></span><script type="math/tex" id="MathJax-Element-4">O(\log_2{n})</script></span></td>
</tr>
<tr class="row-odd">
<td>get</td>
<td><span class="math"><span class="MathJax_Preview"></span><span class="MathJax" id="MathJax-Element-5-Frame" role="textbox" aria-readonly="true"><nobr><span class="math" id="MathJax-Span-30" style="width: 4.921em; display: inline-block;"><span style="display: inline-block; position: relative; width: 4.043em; height: 0px; font-size: 122%;"><span style="position: absolute; clip: rect(1.759em 1000.003em 3.106em -0.465em); top: -2.69em; left: 0.003em;"><span class="mrow" id="MathJax-Span-31"><span class="mi" id="MathJax-Span-32" style="font-family: MathJax_Math; font-style: italic;">O</span><span class="mo" id="MathJax-Span-33" style="font-family: MathJax_Main;">(</span><span class="msubsup" id="MathJax-Span-34"><span style="display: inline-block; position: relative; width: 1.759em; height: 0px;"><span style="position: absolute; clip: rect(1.818em 1000.003em 3.106em -0.524em); top: -2.69em; left: 0.003em;"><span class="mi" id="MathJax-Span-35" style="font-family: MathJax_Main;">log</span><span style="display: inline-block; width: 0px; height: 2.696em;"></span></span><span style="position: absolute; top: -2.046em; left: 1.291em;"><span class="mn" id="MathJax-Span-36" style="font-size: 70.7%; font-family: MathJax_Main;">2</span><span style="display: inline-block; width: 0px; height: 2.286em;"></span></span></span></span><span class="texatom" id="MathJax-Span-37" style="padding-left: 0.179em;"><span class="mrow" id="MathJax-Span-38"><span class="mi" id="MathJax-Span-39" style="font-family: MathJax_Math; font-style: italic;">n</span></span></span><span class="mo" id="MathJax-Span-40" style="font-family: MathJax_Main;">)</span></span><span style="display: inline-block; width: 0px; height: 2.696em;"></span></span></span><span style="border-left-width: 0.004em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 1.361em; vertical-align: -0.354em;"></span></span></nobr></span><script type="math/tex" id="MathJax-Element-5">O(\log_2{n})</script></span></td>
<td><span class="math"><span class="MathJax_Preview"></span><span class="MathJax" id="MathJax-Element-6-Frame" role="textbox" aria-readonly="true"><nobr><span class="math" id="MathJax-Span-41" style="width: 2.52em; display: inline-block;"><span style="display: inline-block; position: relative; width: 2.052em; height: 0px; font-size: 122%;"><span style="position: absolute; clip: rect(1.759em 1000.003em 3.106em -0.465em); top: -2.69em; left: 0.003em;"><span class="mrow" id="MathJax-Span-42"><span class="mi" id="MathJax-Span-43" style="font-family: MathJax_Math; font-style: italic;">O</span><span class="mo" id="MathJax-Span-44" style="font-family: MathJax_Main;">(</span><span class="mn" id="MathJax-Span-45" style="font-family: MathJax_Main;">1</span><span class="mo" id="MathJax-Span-46" style="font-family: MathJax_Main;">)</span></span><span style="display: inline-block; width: 0px; height: 2.696em;"></span></span></span><span style="border-left-width: 0.004em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 1.361em; vertical-align: -0.354em;"></span></span></nobr></span><script type="math/tex" id="MathJax-Element-6">O(1)</script></span></td>
<td><span class="math"><span class="MathJax_Preview"></span><span class="MathJax" id="MathJax-Element-7-Frame" role="textbox" aria-readonly="true"><nobr><span class="math" id="MathJax-Span-47" style="width: 2.638em; display: inline-block;"><span style="display: inline-block; position: relative; width: 2.169em; height: 0px; font-size: 122%;"><span style="position: absolute; clip: rect(1.759em 1000.003em 3.106em -0.465em); top: -2.69em; left: 0.003em;"><span class="mrow" id="MathJax-Span-48"><span class="mi" id="MathJax-Span-49" style="font-family: MathJax_Math; font-style: italic;">O</span><span class="mo" id="MathJax-Span-50" style="font-family: MathJax_Main;">(</span><span class="mi" id="MathJax-Span-51" style="font-family: MathJax_Math; font-style: italic;">n</span><span class="mo" id="MathJax-Span-52" style="font-family: MathJax_Main;">)</span></span><span style="display: inline-block; width: 0px; height: 2.696em;"></span></span></span><span style="border-left-width: 0.004em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 1.361em; vertical-align: -0.354em;"></span></span></nobr></span><script type="math/tex" id="MathJax-Element-7">O(n)</script></span></td>
<td><span class="math"><span class="MathJax_Preview"></span><span class="MathJax" id="MathJax-Element-8-Frame" role="textbox" aria-readonly="true"><nobr><span class="math" id="MathJax-Span-53" style="width: 4.921em; display: inline-block;"><span style="display: inline-block; position: relative; width: 4.043em; height: 0px; font-size: 122%;"><span style="position: absolute; clip: rect(1.759em 1000.003em 3.106em -0.465em); top: -2.69em; left: 0.003em;"><span class="mrow" id="MathJax-Span-54"><span class="mi" id="MathJax-Span-55" style="font-family: MathJax_Math; font-style: italic;">O</span><span class="mo" id="MathJax-Span-56" style="font-family: MathJax_Main;">(</span><span class="msubsup" id="MathJax-Span-57"><span style="display: inline-block; position: relative; width: 1.759em; height: 0px;"><span style="position: absolute; clip: rect(1.818em 1000.003em 3.106em -0.524em); top: -2.69em; left: 0.003em;"><span class="mi" id="MathJax-Span-58" style="font-family: MathJax_Main;">log</span><span style="display: inline-block; width: 0px; height: 2.696em;"></span></span><span style="position: absolute; top: -2.046em; left: 1.291em;"><span class="mn" id="MathJax-Span-59" style="font-size: 70.7%; font-family: MathJax_Main;">2</span><span style="display: inline-block; width: 0px; height: 2.286em;"></span></span></span></span><span class="texatom" id="MathJax-Span-60" style="padding-left: 0.179em;"><span class="mrow" id="MathJax-Span-61"><span class="mi" id="MathJax-Span-62" style="font-family: MathJax_Math; font-style: italic;">n</span></span></span><span class="mo" id="MathJax-Span-63" style="font-family: MathJax_Main;">)</span></span><span style="display: inline-block; width: 0px; height: 2.696em;"></span></span></span><span style="border-left-width: 0.004em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 1.361em; vertical-align: -0.354em;"></span></span></nobr></span><script type="math/tex" id="MathJax-Element-8">O(\log_2{n})</script></span></td>
</tr>
<tr class="row-even">
<td>in</td>
<td><span class="math"><span class="MathJax_Preview"></span><span class="MathJax" id="MathJax-Element-9-Frame" role="textbox" aria-readonly="true"><nobr><span class="math" id="MathJax-Span-64" style="width: 4.921em; display: inline-block;"><span style="display: inline-block; position: relative; width: 4.043em; height: 0px; font-size: 122%;"><span style="position: absolute; clip: rect(1.759em 1000.003em 3.106em -0.465em); top: -2.69em; left: 0.003em;"><span class="mrow" id="MathJax-Span-65"><span class="mi" id="MathJax-Span-66" style="font-family: MathJax_Math; font-style: italic;">O</span><span class="mo" id="MathJax-Span-67" style="font-family: MathJax_Main;">(</span><span class="msubsup" id="MathJax-Span-68"><span style="display: inline-block; position: relative; width: 1.759em; height: 0px;"><span style="position: absolute; clip: rect(1.818em 1000.003em 3.106em -0.524em); top: -2.69em; left: 0.003em;"><span class="mi" id="MathJax-Span-69" style="font-family: MathJax_Main;">log</span><span style="display: inline-block; width: 0px; height: 2.696em;"></span></span><span style="position: absolute; top: -2.046em; left: 1.291em;"><span class="mn" id="MathJax-Span-70" style="font-size: 70.7%; font-family: MathJax_Main;">2</span><span style="display: inline-block; width: 0px; height: 2.286em;"></span></span></span></span><span class="texatom" id="MathJax-Span-71" style="padding-left: 0.179em;"><span class="mrow" id="MathJax-Span-72"><span class="mi" id="MathJax-Span-73" style="font-family: MathJax_Math; font-style: italic;">n</span></span></span><span class="mo" id="MathJax-Span-74" style="font-family: MathJax_Main;">)</span></span><span style="display: inline-block; width: 0px; height: 2.696em;"></span></span></span><span style="border-left-width: 0.004em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 1.361em; vertical-align: -0.354em;"></span></span></nobr></span><script type="math/tex" id="MathJax-Element-9">O(\log_2{n})</script></span></td>
<td><span class="math"><span class="MathJax_Preview"></span><span class="MathJax" id="MathJax-Element-10-Frame" role="textbox" aria-readonly="true"><nobr><span class="math" id="MathJax-Span-75" style="width: 2.52em; display: inline-block;"><span style="display: inline-block; position: relative; width: 2.052em; height: 0px; font-size: 122%;"><span style="position: absolute; clip: rect(1.759em 1000.003em 3.106em -0.465em); top: -2.69em; left: 0.003em;"><span class="mrow" id="MathJax-Span-76"><span class="mi" id="MathJax-Span-77" style="font-family: MathJax_Math; font-style: italic;">O</span><span class="mo" id="MathJax-Span-78" style="font-family: MathJax_Main;">(</span><span class="mn" id="MathJax-Span-79" style="font-family: MathJax_Main;">1</span><span class="mo" id="MathJax-Span-80" style="font-family: MathJax_Main;">)</span></span><span style="display: inline-block; width: 0px; height: 2.696em;"></span></span></span><span style="border-left-width: 0.004em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 1.361em; vertical-align: -0.354em;"></span></span></nobr></span><script type="math/tex" id="MathJax-Element-10">O(1)</script></span></td>
<td><span class="math"><span class="MathJax_Preview"></span><span class="MathJax" id="MathJax-Element-11-Frame" role="textbox" aria-readonly="true"><nobr><span class="math" id="MathJax-Span-81" style="width: 2.638em; display: inline-block;"><span style="display: inline-block; position: relative; width: 2.169em; height: 0px; font-size: 122%;"><span style="position: absolute; clip: rect(1.759em 1000.003em 3.106em -0.465em); top: -2.69em; left: 0.003em;"><span class="mrow" id="MathJax-Span-82"><span class="mi" id="MathJax-Span-83" style="font-family: MathJax_Math; font-style: italic;">O</span><span class="mo" id="MathJax-Span-84" style="font-family: MathJax_Main;">(</span><span class="mi" id="MathJax-Span-85" style="font-family: MathJax_Math; font-style: italic;">n</span><span class="mo" id="MathJax-Span-86" style="font-family: MathJax_Main;">)</span></span><span style="display: inline-block; width: 0px; height: 2.696em;"></span></span></span><span style="border-left-width: 0.004em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 1.361em; vertical-align: -0.354em;"></span></span></nobr></span><script type="math/tex" id="MathJax-Element-11">O(n)</script></span></td>
<td><span class="math"><span class="MathJax_Preview"></span><span class="MathJax" id="MathJax-Element-12-Frame" role="textbox" aria-readonly="true"><nobr><span class="math" id="MathJax-Span-87" style="width: 4.921em; display: inline-block;"><span style="display: inline-block; position: relative; width: 4.043em; height: 0px; font-size: 122%;"><span style="position: absolute; clip: rect(1.759em 1000.003em 3.106em -0.465em); top: -2.69em; left: 0.003em;"><span class="mrow" id="MathJax-Span-88"><span class="mi" id="MathJax-Span-89" style="font-family: MathJax_Math; font-style: italic;">O</span><span class="mo" id="MathJax-Span-90" style="font-family: MathJax_Main;">(</span><span class="msubsup" id="MathJax-Span-91"><span style="display: inline-block; position: relative; width: 1.759em; height: 0px;"><span style="position: absolute; clip: rect(1.818em 1000.003em 3.106em -0.524em); top: -2.69em; left: 0.003em;"><span class="mi" id="MathJax-Span-92" style="font-family: MathJax_Main;">log</span><span style="display: inline-block; width: 0px; height: 2.696em;"></span></span><span style="position: absolute; top: -2.046em; left: 1.291em;"><span class="mn" id="MathJax-Span-93" style="font-size: 70.7%; font-family: MathJax_Main;">2</span><span style="display: inline-block; width: 0px; height: 2.286em;"></span></span></span></span><span class="texatom" id="MathJax-Span-94" style="padding-left: 0.179em;"><span class="mrow" id="MathJax-Span-95"><span class="mi" id="MathJax-Span-96" style="font-family: MathJax_Math; font-style: italic;">n</span></span></span><span class="mo" id="MathJax-Span-97" style="font-family: MathJax_Main;">)</span></span><span style="display: inline-block; width: 0px; height: 2.696em;"></span></span></span><span style="border-left-width: 0.004em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 1.361em; vertical-align: -0.354em;"></span></span></nobr></span><script type="math/tex" id="MathJax-Element-12">O(\log_2{n})</script></span></td>
</tr>
<tr class="row-odd">
<td>del</td>
<td><span class="math"><span class="MathJax_Preview"></span><span class="MathJax" id="MathJax-Element-13-Frame" role="textbox" aria-readonly="true"><nobr><span class="math" id="MathJax-Span-98" style="width: 3.106em; display: inline-block;"><span style="display: inline-block; position: relative; width: 2.52em; height: 0px; font-size: 122%;"><span style="position: absolute; clip: rect(1.759em 1000.003em 3.106em -0.465em); top: -2.69em; left: 0.003em;"><span class="mrow" id="MathJax-Span-99"><span class="mi" id="MathJax-Span-100" style="font-family: MathJax_Math; font-style: italic;">O</span><span class="mo" id="MathJax-Span-101" style="font-family: MathJax_Main;">(</span><span class="mi" id="MathJax-Span-102" style="font-family: MathJax_Math; font-style: italic;">n</span><span class="mo" id="MathJax-Span-103" style="font-family: MathJax_Main;">)</span><span class="mo" id="MathJax-Span-104" style="font-family: MathJax_Main;">)</span></span><span style="display: inline-block; width: 0px; height: 2.696em;"></span></span></span><span style="border-left-width: 0.004em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 1.361em; vertical-align: -0.354em;"></span></span></nobr></span><script type="math/tex" id="MathJax-Element-13">O(n))</script></span></td>
<td><span class="math"><span class="MathJax_Preview"></span><span class="MathJax" id="MathJax-Element-14-Frame" role="textbox" aria-readonly="true"><nobr><span class="math" id="MathJax-Span-105" style="width: 2.52em; display: inline-block;"><span style="display: inline-block; position: relative; width: 2.052em; height: 0px; font-size: 122%;"><span style="position: absolute; clip: rect(1.759em 1000.003em 3.106em -0.465em); top: -2.69em; left: 0.003em;"><span class="mrow" id="MathJax-Span-106"><span class="mi" id="MathJax-Span-107" style="font-family: MathJax_Math; font-style: italic;">O</span><span class="mo" id="MathJax-Span-108" style="font-family: MathJax_Main;">(</span><span class="mn" id="MathJax-Span-109" style="font-family: MathJax_Main;">1</span><span class="mo" id="MathJax-Span-110" style="font-family: MathJax_Main;">)</span></span><span style="display: inline-block; width: 0px; height: 2.696em;"></span></span></span><span style="border-left-width: 0.004em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 1.361em; vertical-align: -0.354em;"></span></span></nobr></span><script type="math/tex" id="MathJax-Element-14">O(1)</script></span></td>
<td><span class="math"><span class="MathJax_Preview"></span><span class="MathJax" id="MathJax-Element-15-Frame" role="textbox" aria-readonly="true"><nobr><span class="math" id="MathJax-Span-111" style="width: 2.638em; display: inline-block;"><span style="display: inline-block; position: relative; width: 2.169em; height: 0px; font-size: 122%;"><span style="position: absolute; clip: rect(1.759em 1000.003em 3.106em -0.465em); top: -2.69em; left: 0.003em;"><span class="mrow" id="MathJax-Span-112"><span class="mi" id="MathJax-Span-113" style="font-family: MathJax_Math; font-style: italic;">O</span><span class="mo" id="MathJax-Span-114" style="font-family: MathJax_Main;">(</span><span class="mi" id="MathJax-Span-115" style="font-family: MathJax_Math; font-style: italic;">n</span><span class="mo" id="MathJax-Span-116" style="font-family: MathJax_Main;">)</span></span><span style="display: inline-block; width: 0px; height: 2.696em;"></span></span></span><span style="border-left-width: 0.004em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 1.361em; vertical-align: -0.354em;"></span></span></nobr></span><script type="math/tex" id="MathJax-Element-15">O(n)</script></span></td>
<td><span class="math"><span class="MathJax_Preview"></span><span class="MathJax" id="MathJax-Element-16-Frame" role="textbox" aria-readonly="true"><nobr><span class="math" id="MathJax-Span-117" style="width: 4.921em; display: inline-block;"><span style="display: inline-block; position: relative; width: 4.043em; height: 0px; font-size: 122%;"><span style="position: absolute; clip: rect(1.759em 1000.003em 3.106em -0.465em); top: -2.69em; left: 0.003em;"><span class="mrow" id="MathJax-Span-118"><span class="mi" id="MathJax-Span-119" style="font-family: MathJax_Math; font-style: italic;">O</span><span class="mo" id="MathJax-Span-120" style="font-family: MathJax_Main;">(</span><span class="msubsup" id="MathJax-Span-121"><span style="display: inline-block; position: relative; width: 1.759em; height: 0px;"><span style="position: absolute; clip: rect(1.818em 1000.003em 3.106em -0.524em); top: -2.69em; left: 0.003em;"><span class="mi" id="MathJax-Span-122" style="font-family: MathJax_Main;">log</span><span style="display: inline-block; width: 0px; height: 2.696em;"></span></span><span style="position: absolute; top: -2.046em; left: 1.291em;"><span class="mn" id="MathJax-Span-123" style="font-size: 70.7%; font-family: MathJax_Main;">2</span><span style="display: inline-block; width: 0px; height: 2.286em;"></span></span></span></span><span class="texatom" id="MathJax-Span-124" style="padding-left: 0.179em;"><span class="mrow" id="MathJax-Span-125"><span class="mi" id="MathJax-Span-126" style="font-family: MathJax_Math; font-style: italic;">n</span></span></span><span class="mo" id="MathJax-Span-127" style="font-family: MathJax_Main;">)</span></span><span style="display: inline-block; width: 0px; height: 2.696em;"></span></span></span><span style="border-left-width: 0.004em; border-left-style: solid; display: inline-block; overflow: hidden; width: 0px; height: 1.361em; vertical-align: -0.354em;"></span></span></nobr></span><script type="math/tex" id="MathJax-Element-16">O(\log_2{n})</script></span></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
    </div>
  </div>

    </div>
    <aside class="postpromonav"><nav><ul itemprop="keywords" class="tags">
<li><a class="tag p-category" href="../categories/chs.html" rel="tag">CHS</a></li>
            <li><a class="tag p-category" href="../categories/ipython.html" rel="tag">ipython</a></li>
            <li><a class="tag p-category" href="../categories/other.html" rel="tag">Other</a></li>
            <li><a class="tag p-category" href="../categories/python.html" rel="tag">Python</a></li>
        </ul>
<ul class="pager hidden-print">
<li class="previous">
                <a href="kivy-ch5-remote-desktop-app.html" rel="prev" title="kivy-ch5-remote-desktop-app">Previous post</a>
            </li>
            <li class="next">
                <a href="kivy-ch9-shmup-app.html" rel="next" title="kivy-ch9-shmup-app">Next post</a>
            </li>
        </ul></nav></aside><script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"> </script><script type="text/x-mathjax-config">
MathJax.Hub.Config({
    tex2jax: {
        inlineMath: [ ['$','$'], ["\\(","\\)"] ],
        displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
        processEscapes: true
    },
    displayAlign: 'center', // Change this to 'center' to center equations.
    "HTML-CSS": {
        styles: {'.MathJax_Display': {"margin": 0}}
    }
});
</script></article>
</div>
        <!--End of body content-->

        <footer id="footer">
            Contents © 2017         <a href="mailto:muxuezi@gmail.com">Tao Junjie</a> - Powered by         <a href="https://getnikola.com" rel="nofollow">Nikola</a>         
<a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0">
<img alt="Creative Commons License BY-NC-SA" style="border-width:0; margin-bottom:12px;" src="http://i.creativecommons.org/l/by-nc-sa/4.0/80x15.png"></a>
            
        </footer>
</div>
</div>


            <script src="../assets/js/all-nocdn.js"></script><script>$('a.image-reference:not(.islink) img:not(.islink)').parent().colorbox({rel:"gal",maxWidth:"100%",maxHeight:"100%",scalePhotos:true});</script><!-- fancy dates --><script>
    moment.locale("en");
    fancydates(0, "YYYY-MM-DD HH:mm");
    </script><!-- end fancy dates --><script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-51330059-1', 'auto');
  ga('send', 'pageview');

</script>
</body>
</html>
